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: (

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"));


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