Saturday, August 25, 2012

Find Number of duplicates present in a ArrayList

To find the number of occurrences of duplicate integer present in List, first we will create temp List which will have no duplicates and then use collection frequecy() function to get the occurrence of duplicate number in original list.
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(67);
list.add(32);
list.add(6);
list.add(4);
list.add(67);
list.add(3);
list.add(6);
list.add(2);
list.add(6);
list.add(2);
list.add(67);
list.add(6);
list.add(4);
Set<Integer> filterList = new HashSet<Integer>(list);
Iterator<Integer> iterator = filterList.iterator();
while(iterator.hasNext()){
int number = iterator.next();
int occurrence = Collections.frequency(list, number);
System.out.println("| Number \t"+number+"\t| occures : \t"+occurrence+" Times |");
System.out.println(" ------------------------------------------------");
}
}
view raw sample.java hosted with ❤ by GitHub

0 comments:

Post a Comment