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 the Author

author image
mkyong
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

avatar
23 Comment threads
7 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
25 Comment authors
rippersubhashsatishsanketHero Recent comment authors
newest oldest most voted
Hero
Guest
Hero

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

ripper
Guest
ripper

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

subhash
Guest
subhash

how to find duplicate inside object in inner object

satish
Guest
satish

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

sanket
Guest
sanket

Thank you . Superb . very much useful .

Dinesh Krishnan
Guest
Dinesh Krishnan

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

shravan
Guest
shravan

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
Guest
Akash Sengar

thanks

Emir
Guest
Emir

thanks, this is very helpful

strangejan11
Guest
strangejan11

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

Siva Munnaluri
Guest
Siva Munnaluri

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
Guest
Amar

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

Hunaid
Guest
Hunaid
Prateek Ashtikar
Guest
Prateek Ashtikar

Very nice example. Many thanks :)

garima
Guest
garima

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

Anirban Datta
Guest
Anirban Datta

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
Guest
Adish Upadhyay

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”); } } }

trackback
How to join two Lists in Java

[…] How To Count Duplicated Items In Java List […]

ginanjar
Guest
ginanjar

thanks for sharing, it’s work ;)

elvis
Guest
elvis

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

Mohan
Guest
Mohan

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

trackback
Menghitung duplikasi item list di java | Keep it simple

[…] dan terus mencari cara untuk memecahkan itu soal. Setelah nyari sana sini ternyata tutornya ada di http://www.mkyong.com/java/how-to-count-duplicated-items-in-java-list/ dan tinggal dioprek sedikit dan kode itu akhirnya dipecahkan Soalnya sangat simpel jadi kita […]

jssay
Guest
jssay

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

Tabs
Guest
Tabs

Why

sam
Guest
sam

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

sindhusha
Guest
sindhusha

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