How to count occurrences of each duplicate element in a list
{a,b,d,c,a,b} ?
Thanks in Advance
Answer Posted / pratiksha
import java.util.*;
class alpha
{
String let;
public alpha(String l)
{
this.let=l;
}
}
public class Duplicateletters
{
public static void main(String []args)
{
int i=0;
String[] letters = {"a", "b", "b", "c" ,"a", "d","a"};
ArrayList<alpha> a = new ArrayList<alpha>();
for(i=0;i<letters.length;i++)
{
a.add(new alpha(letters[i]));
}
String let1=null;
Iterator<alpha> i1 = a.iterator();
while(i1.hasNext())
{
alpha a1 = i1.next();
let1=a1.let;
}
int j=0;
Iterator<alpha> i2 = a.iterator();
while(i2.hasNext())
{
alpha a2 = i2.next();
if(let1==a2.let)
{
j=j+1;
}
}
System.out.println(j);
}
}
| Is This Answer Correct ? | 3 Yes | 1 No |
Post New Answer View All Answers
What is formatted output in java?
Can a private method be declared as static?
What is the use of parse function in java?
What are anonymous inner classes?
What is array command?
What is constructor chaining and how is it achieved in java?
What do you mean by compiler?
Can we use a switch statement with strings?
Name few java util classes introduced with java 8 ?
Class c implements interface I containing method m1 and m2 declarations. Class c has provided implementation for method m2. Can I create an object of class c?
What is the maximum size of arraylist in java?
Why we go for collections in java?
Can a abstract class be declared final?
Are arrays primitive data types?
Is 0 a real number?