Learnitweb

Count duplicate elements in ArrayList in Java

Problem statement

For a given list of elements (String elements in our example), the task is to find which elements are duplicate and the number of times the element appears in the list.

List

“one”, “two”, “three”, “one”, “three”, “four”, “five”, “six”, “one”, “nine”, “six”

Output

nine : 1
six : 2
four : 1
one : 3
five : 1
three : 2
two : 1

Approach1: Using Collectors.groupingBy and Collectors.counting()

In this approach, we’ll create a Map with key as the string and value as the count of number of times string appears in the list. We’ll use Collectors.groupingBy and Collectors.counting to group and count the strings in the stream created from list.

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class CountDuplicateExample {

	public static void main(final String[] arguments) {
		List<String> list = Arrays.asList("one", "two", "three", "one", "three", "four", "five", "six", "one", "nine",
				"six");
		// we can also use Function.identity() instead of item->item
		Map<String, Long> map = list.stream().collect(Collectors.groupingBy(item -> item, Collectors.counting()));

		map.forEach((k, v) -> System.out.println(k + " : " + v));
	}
}

Output

nine : 1
six : 2
four : 1
one : 3
five : 1
three : 2
two : 1

Method 2: Using iterator

We’ll create an empty map. We’ll iterate the list and for each element of the list, we’ll check if the element is present in the map. If key is not found, insert in the map with count as 1. If the key is found, increase the count by 1.

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class CountDuplicateExample {

	public static void main(final String[] arguments) {
		// create list
		List<String> list = Arrays.asList("one", "two", "three", "one", "three", "four", "five", "six", "one", "nine",
				"six");

		// create empty map to save strings and count
		Map<String, Integer> map = new HashMap<>();

		for (String element : list) {
			// if map contains key, increase count by 1
			if (map.containsKey(element)) {
				map.put(element, map.get(element) + 1);
			} else { // if key not found, put string in map with count 1
				map.put(element, 1);
			}
		}

		// iterate and print elements of map
		Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
		for (Map.Entry<String, Integer> entry : entrySet) {
			System.out.println(entry.getKey() + " : " + entry.getValue());
		}
	}
}

Approach 3: Using Collections.frequency()

We’ll create a Set from the list. This will remove the duplicate elements. We’ll then iterate the Set and for each value, we’ll check the number of times the string appears in the list using Collections.frequency() method.

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CountDuplicateExample {

	public static void main(final String[] arguments) {
		// create list
		List<String> list = Arrays.asList("one", "two", "three", "one", "three", "four", "five", "six", "one", "nine",
				"six");

		// create a set from the list, this will remove duplicates
		Set<String> set = new HashSet<String>(list);
		for (String r : set) {
			System.out.println(r + ": " + Collections.frequency(list, r));
		}
	}
}