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

Related Posts:

  • Whois domain detail by javaApache commmon org.apache.commons.net.whois.WhoisClient class provides the domain details. Following details: Domain Name: UTTESH.COM Registrar: GODADDY.COM, LLC Sponsoring Registrar IANA ID: 146 Whois Server: whois.godaddy… Read More
  • Screen capture by Java robot classCame across a rare util class of java "Robot" class, which provie the control/auto typing of letter/keyboard controll and screen capering. sample code … Read More
  • 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 or… Read More
  • Timezone Conversion with Daylight savingTimezone conversion from one timezone to other timezone for the given time or hour string input. While converting the timezone we have to consider DST standard also. in jdk 1.3. and 1.4 DST was not taken care internally by j… Read More
  • Is in Daylight Saving Time (DST)? for given date and timezone Daylight Saving Time (DST) is the practice of turning the clock ahead as warmer weather approaches and back as it becomes colder again so that people will have one more hour of daylight in the afternoon and evening during th… Read More

0 comments:

Post a Comment