javacodex.com
Java Examples
Java Examples
memu home questions

Count Duplicated Items In A List

This java example shows you how to count the total number of duplicated elements in a list.

Source: (CountDuplicates.java)

import java.util.*;
 
public class CountDuplicates {
   public static void main( String[] args ) {
 
      ArrayList list = new ArrayList();
      list.add( "cat" );
      list.add( "dog" );
      list.add( "pig" );
      list.add( "cow" );
      list.add( "pig" );
      System.out.println("list: " + list);
      System.out.println("pig count: " + Collections.frequency(list, "pig"));
   }
}
 

Output:

# java CountDuplicates 
list: [cat, dog, pig, cow, pig]
pig count: 2

Contact: javacodex@yahoo.com