Main Tutorials

How to count duplicated items in Java List

A Java example to show you how to count the total number of duplicated entries in a List, using Collections.frequency and Map.

CountDuplicatedList.java

package com.mkyong;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class CountDuplicatedList {

  public static void main(String[] args) {

	List<String> list = new ArrayList<String>();
	list.add("a");
	list.add("b");
	list.add("c");
	list.add("d");
	list.add("b");
	list.add("c");
	list.add("a");
	list.add("a");
	list.add("a");

	System.out.println("\nExample 1 - Count 'a' with frequency");
	System.out.println("a : " + Collections.frequency(list, "a"));

	System.out.println("\nExample 2 - Count all with frequency");
	Set<String> uniqueSet = new HashSet<String>(list);
	for (String temp : uniqueSet) {
		System.out.println(temp + ": " + Collections.frequency(list, temp));
	}

	System.out.println("\nExample 3 - Count all with Map");
	Map<String, Integer> map = new HashMap<String, Integer>();

	for (String temp : list) {
		Integer count = map.get(temp);
		map.put(temp, (count == null) ? 1 : count + 1);
	}
	printMap(map);
				
	System.out.println("\nSorted Map");
	Map<String, Integer> treeMap = new TreeMap<String, Integer>(map);
	printMap(treeMap);
		
  }
	
  public static void printMap(Map<String, Integer> map){

	for (Map.Entry<String, Integer> entry : map.entrySet()) {
		System.out.println("Key : " + entry.getKey() + " Value : "
			+ entry.getValue());
	}
		
  }

}

Output


Example 1 - Count 'a' with frequency
a : 4

Example 2 - Count all with frequency
d: 1
b: 2
c: 2
a: 4

Example 3 - Count all with Map
Key : d Value : 1
Key : b Value : 2
Key : c Value : 2
Key : a Value : 4

Sorted Map
Key : a Value : 4
Key : b Value : 2
Key : c Value : 2
Key : d Value : 1

References

  1. Collections.frequency JavaDoc
  2. How to sort a Map in Java

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
30 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Hero
6 years ago

What if I want to do the same with Java8 stream api????

ripper
5 years ago
Reply to  Hero

myUniqueHashSet.stream().forEach(item -> {myEmptyHashMap.put(item, Collections.frequency(myArrayList,item));})

sam
3 years ago

why we use tree map after the hashmap can anybody explain ?

subhash
5 years ago

how to find duplicate inside object in inner object

satish
5 years ago

after converting list into set, the duplicates will gone right??

hggiki
4 years ago
Reply to  satish

yup

sanket
5 years ago

Thank you . Superb . very much useful .

Dinesh Krishnan
6 years ago

Hi, It is the very good post. Thanks for sharing.

shravan
6 years ago

Collections.sort(list);
for(int i=0;i<list.size();i++)
{
int k=list.lastIndexOf(list.get(i));
System.out.println(list.get(i)+" "+(k-list.indexOf(list.get(i))+1));
i=k;
}

Akash Sengar
7 years ago

thanks

Emir
8 years ago

thanks, this is very helpful

strangejan11
8 years ago

hey how to show only the items that got the highest number/duplication ?? thanks !! 😀

Siva Munnaluri
8 years ago

while removing the duplicate element in the list using the hashmap ,
I am getting the NullPointerException at “Integer count = map.get(temp);”
I modified little bit of the code
for(String st : list)
{
if(map.containsKey(st))
{
map.put(st,map.get(st)+1 );
}else
{
map.put(st,1);
}
}

Amar
8 years ago

Excellent logic in foreach loop with map. Kudos!!!

Hunaid
9 years ago
Prateek Ashtikar
9 years ago

Very nice example. Many thanks 🙂

garima
9 years ago

Hi,how to deal with with it if case should not be considered while counting..In this case it will

Anirban Datta
9 years ago

Hi, Is it possible to keep the highest value along with the count? Suppose I have a collection like this
[File1, session1,transformation1,Data, 82345]
[File1, session1,transformation1,Data, 92345]

[File2, session2,transformation1,Data, 72345]

[File2, session2,transformation1,Data, 52345]

[File2, session2,transformation2,Data, 62345]

[File2, session2,transformation2,Data, 102345]

The resultant collection would be like
[File1, session1,transformation1,Data, 92345,2]

[File2, session2,transformation1,Data, 72345,2]

[File2, session2,transformation2,Data, 102345,2]

The max based on the sorting of all the columns and their count of occurrence.

Adish Upadhyay
10 years ago

public class CountDuplicateinArrayList
{

public static void main(String[] args)
{
int temp=0;
ArrayList al= new ArrayList();
al.add(“A”);
al.add(“B”);
al.add(“A”);
al.add(“B”);
al.add(“B”);

al.add(“A”);
al.add(“B”);
al.add(“A”);
al.add(“B”);
al.add(“B”);

int a=al.size();

Set s= new TreeSet(al);
int b=a-s.size();
System.out.println(” No of Dubplicate is:”+b);

Map wordMap = new HashMap();

Iterator it=al.iterator();
while(it.hasNext())
{
String ap=(String)it.next();
if(wordMap.containsKey(ap))
{
wordMap.put(ap, wordMap.get(ap)+1);
}
else
wordMap.put(ap, temp+1);
}

Set s1=wordMap.entrySet();
Iterator it1=s1.iterator();
while(it1.hasNext())
{
Map.Entry m=(Map.Entry)it1.next();

System.out.println(m.getKey()+” Comes :”+m.getValue()+” Times”);
}

}
}

ginanjar
10 years ago

thanks for sharing, it’s work 😉

elvis
10 years ago

Exactly what I was looking for!
The Collections class is full of nice feature.

Mohan
11 years ago

check this………..

 import java.util.*;

class ArrayListTest{
    public static void main(String args[]){

    ArrayList a=new ArrayList(5);
       a.add(1);
       a.add(2);
       a.add(3);
       a.add(4);
       a.ensureCapacity(5);
       System.out.println(a.size());
       a.add(5);
       a.add(1);
       System.out.println(a.size());
       a.add(1);
       a.add(2);
       a.add(3);
       a.add(4);
       System.out.println(a.size());
       a.add(5);
       a.add(1);
   
     System.out.println(a.size());
     System.out.println(a.isEmpty());
     System.out.println(a);
     
     System.out.println(a);
     System.out.println(a.get(1));
     System.out.println(a.contains("Mohan"));
     System.out.println(a.indexOf("Mohan"));
      
     HashSet h=new HashSet(a);
     System.out.println(h);
     System.out.println(a.size()-h.size());
     //h.size();
      
    a.clear();
     
     System.out.println(a.isEmpty());
    
    }
    
}

Output

4
6
10
12
false
[1, 2, 3, 4, 5, 1, 1, 2, 3, 4, 5, 1]
[1, 2, 3, 4, 5, 1, 1, 2, 3, 4, 5, 1]
2
false
-1
[1, 2, 3, 4, 5]
7
true
jssay
11 years ago

never use Collections.frequency to calculate the duplicated collection elements, thanks for sharing.

Tabs
11 years ago
Reply to  jssay

Why

sam
10 years ago
Reply to  Tabs

why not to use collections frequency method… any disadvantages or side-effects ???

sindhusha
6 years ago

HI,I am having a arraylist ArrayList data=new ArrayList();
my arraylist data is
[{“id”:”33500010060031″,”data”:”Gudlavalleru”},{“id”:”33500300090003″,”data”:”Kukatpally”},{“id”:”33500300020020″,”data”:”Nampally”},{“id”:”33500300020020″,”data”:”Nampally”},{“id”:”33500300020020″,”data”:”Nampally”},{“id”:”33500300020020″,”data”:”Nampally”},{“id”:”33500300020020″,”data”:”Nampally”}]

Now i want to the no of occurances for location?
after setting the data in array list with duplicate values i want to check the no of occurances but its not finding out..Can you give me solution?
Set uniqueSet = new HashSet(data);
System.out.println(“unique set is…”+PlatformFactory.toJson(uniqueSet));
for (DetailsTo temp : uniqueSet) {
System.out.println(PlatformFactory.toJson(temp));
dataMap.put(temp.getData(), Collections.frequency(data,temp));
System.out.println(temp + “: ” + Collections.frequency(data, temp));
}

output is:
{“id”:”33500300020020″,”data”:”Nampally”}: 1
{“id”:”33500300090003″,”data”:”Kukatpally”}: 1
{“id”:”33500300020020″,”data”:”Nampally”}: 1
{“id”:”33500300020020″,”data”:”Nampally”}: 1
{“id”:”33500010060031″,”data”:”Gudlavalleru”}: 1
{“id”:”33500300020020″,”data”:”Nampally”}: 1
{“id”:”33500300020020″,”data”:”Nampally”}: 1