java Find the duplicate value in the arraylist?

0 votes
32 views
asked May 11 in JAVA by admin (8,115 points)
HI

   Please provide the sample to get the deublicate value from the Integer ArrayList

1 Answer

0 votes
answered May 11 by codeg (2,600 points)
package com.mkyong;
import java.util.*

public class CountDuplicatedList {
  public static void main(String[] args) {
	List<String> list = new ArrayList<String>();
	list.add("a","b","c","d","b","c","a","a","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());
	}
  }
}
Reference : https://www.mkyong.com/java/how-to-count-duplicated-items-in-java-list/
...