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.
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:
- Import Statements: We import the necessary classes, including
ArrayList,List,HashMap, andMapfrom thejava.utilpackage. - Creating the List: We create an
ArrayListnamednumbersand populate it with some integers, including duplicates. - Printing the Original List: We print the contents of the
numberslist usingSystem.out.println(). - Finding Duplicate Numbers: We call the
findDuplicates()method to find the duplicate elements in thenumberslist. ThefindDuplicates()method takes a genericListas input and returns a list of duplicate elements. findDuplicates()Method: This method uses aHashMapnamedelementCountMapto store the count of each element in the input list.- Counting Elements: We iterate through the input list using a
forloop. For each element, we update its count in theelementCountMap. 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. - Identifying Duplicate Elements: After counting the elements, we iterate through the
elementCountMapusing aforloop. For each entry in the map, if the count is greater than 1, it means the element is a duplicate. We add it to theduplicateslist. - Returning the Duplicate List: The method returns the
duplicateslist, which contains the duplicate elements from the input list. - Printing Duplicate Numbers: We print the contents of the
duplicateNumberslist usingSystem.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.
