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


Please Help Members By Posting Answers For Below Questions

which class is the wait() method defined in? : Java thread

602


What is java lang string?

650


Write a program to show whether a graph is a tree or not using adjacency matrix.

703


Which container method is used to cause a container to be laid out and redisplayed in java programming?

712


What is the difference between equals() and == in java?

600






What is an interface in java?

659


What is the difference between stringbuffer and stringbuilder class?

665


What is a numeric format?

656


This abstract class is correct ? abstract class A { public abstract void Disp(); public abstract void B() { } public absract virtual C() { } }

653


What’s the difference between unit, integration and functional testing?

706


Can you use this() and super() both in a constructor?

616


Which sort is best in java?

622


Where local and global variables are stored?

645


Can a class be private or protected in java?

654


What is thread synchronization in java?

572