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:

  • Spring + JPA + Hibernate Sample IntroductionStand-alone Spring application sample, Spring framework supports the persistence layer to be fully implemented through JPA. Simple example of configure the Spring needed components to perform persistence over sta… Read More
  • Java get system environment variable detailsGet System environment variable details. import java.util.Map; /** * * @author Uttesh Kumar T.H. */ public class FindJdk { public static void main(String[] args) { Map<String, String> env = System.gete… Read More
  • Generate PDF page as imageWe can generate the image of pdf page by using the Pdf-renderer Simple class which generate the images file of given pdf file. /* * To change this license header, choose License Headers in Project Properties. * To change … Read More
  • find character uppercase/lowercase count from given textFind the character upper/lower case occurrence count with simple single line code without iteration in java. int counta = text.split("(?=[a])").length - 1; This simple code will find all the lowercase character 'a… Read More
  • Reverse elements in ArrayReverse all elements in array, traditional way how we use to do is get the array and iterate through the for loop and use swap logic to move to temporary array and get the result. But from jdk 1.5 introduced reverse() in ja… Read More

0 comments:

Post a Comment