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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(" ------------------------------------------------"); | |
} | |
} |
0 comments:
Post a Comment