Find duplicate elements in an ArrayList

Problem Statement: Implement a method to find duplicate elements in an ArrayList in Java.

Here’s a Java program that demonstrates how to implement a method to find duplicate elements in an ArrayList. I’ll provide a step-by-step explanation of the logic used in the program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.javacodepoint.collection;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class ArrayListFindDuplicates {
 
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(2);
        numbers.add(4);
        numbers.add(1);
 
        List<Integer> duplicateNumbers = findDuplicates(numbers);
 
        System.out.println("Original List: " + numbers);
        System.out.println("Duplicate Numbers: " + duplicateNumbers);
    }
 
    // Method to find duplicate elements in an ArrayList
    public static <T> List<T> findDuplicates(List<T> list) {
        List<T> duplicates = new ArrayList<>();
        Map<T, Integer> elementCountMap = new HashMap<>();
 
        for (T element : list) {
            elementCountMap.put(element, elementCountMap.getOrDefault(element, 0) + 1);
        }
 
        for (Map.Entry<T, Integer> entry : elementCountMap.entrySet()) {
            if (entry.getValue() > 1) {
                duplicates.add(entry.getKey());
            }
        }
 
        return duplicates;
    }
}

OUTPUT:

Original List: [1, 2, 3, 2, 4, 1]
Duplicate Numbers: [1, 2]

Explanation:

  1. Import Statements: We import the necessary classes, including ArrayList, List, HashMap, and Map from the java.util package.
  2. Creating the List: We create an ArrayList named numbers and populate it with some integers, including duplicates.
  3. Printing the Original List: We print the contents of the numbers list using System.out.println().
  4. Finding Duplicate Numbers: We call the findDuplicates() method to find the duplicate elements in the numbers list. The findDuplicates() method takes a generic List as input and returns a list of duplicate elements.
  5. findDuplicates() Method: This method uses a HashMap named elementCountMap to store the count of each element in the input list.
  6. Counting Elements: We iterate through the input list using a for loop. For each element, we update its count in the elementCountMap. If the element doesn’t exist in the map, we add it with a count of 1. If it already exists, we increment its count.
  7. Identifying Duplicate Elements: After counting the elements, we iterate through the elementCountMap using a for loop. For each entry in the map, if the count is greater than 1, it means the element is a duplicate. We add it to the duplicates list.
  8. Returning the Duplicate List: The method returns the duplicates list, which contains the duplicate elements from the input list.
  9. Printing Duplicate Numbers: We print the contents of the duplicateNumbers list using System.out.println().

By following these steps, the program demonstrates how to find duplicate elements in an ArrayList by using a HashMap to count the occurrences of each element and then identify elements with counts greater than 1.

See also: ArrayList Manipulation: add, remove Elements, find size.

Java logical programs list


Java Basic Programs

Java String Programs

Java String Array Programs

Java Miscellaneous Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *