count duplicate elements in a list
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   java_learner
Posted On:   Wednesday, September 29, 2010 08:48 AM

Hi All,


I have a list which consists of duplicate elements

for ex: list {a,b,d,a,d,f,b}

How to count the number of occurrences of duplicate
elements in a list ?


Thanks in Advance

Re: count duplicate elements in a list

Posted By:   amitg  
Posted On:   Monday, August 6, 2012 08:41 PM

better you read the size of list and then put all the element in set and again read the size and do the delta which is nothing but your count of duplicate element

count duplicate elements in a list

Posted By:   G_Muraliraj  
Posted On:   Monday, November 22, 2010 02:50 AM

 Hi I think it is usefull to u.. Thank you 



import java.util.*;
public class LL
{
public static void main(String[] args)
{

List ll=new LinkedList();
ll.add(new Character('a'));
ll.add(new Character('b'));
ll.add(new Character('d'));
ll.add(new Character('a'));
ll.add(new Character('d'));
ll.add(new Character('f'));
ll.add(new Character('b'));
System.out.println("Content ="+ll);
System.out.println("Size ="+ll.size());



int a=0;
ListIterator litr1=ll.listIterator();
ListIterator litr2=ll.listIterator();

while(litr1.hasNext())
{
Object obj1=litr1.next();
Character str1=(Character)obj1;
char ch1=str1.charValue();

while(litr2.hasNext())
{
Object obj2=litr2.next();
Character str2=(Character)obj2;
char ch2=str2.charValue();

if(ch1==ch2)
{

a++;
}
}
System.out.println(ch1+" is repeated in "+a+" Times");
a=0;
while(litr2.hasPrevious())
{
Object o=litr2.previous();
}


}

}
}

Re: count duplicate elements in a list

Posted By:   Anonymous  
Posted On:   Tuesday, November 9, 2010 07:19 AM

hi
About | Sitemap | Contact