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:
- Import Statements: We import the necessary classes, including
ArrayList
,List
,HashMap
, andMap
from thejava.util
package. - Creating the List: We create an
ArrayList
namednumbers
and populate it with some integers, including duplicates. - Printing the Original List: We print the contents of the
numbers
list usingSystem.out.println()
. - Finding Duplicate Numbers: We call the
findDuplicates()
method to find the duplicate elements in thenumbers
list. ThefindDuplicates()
method takes a genericList
as input and returns a list of duplicate elements. findDuplicates()
Method: This method uses aHashMap
namedelementCountMap
to store the count of each element in the input list.- Counting Elements: We iterate through the input list using a
for
loop. 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
elementCountMap
using afor
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 theduplicates
list. - Returning the Duplicate List: The method returns the
duplicates
list, which contains the duplicate elements from the input list. - Printing Duplicate Numbers: We print the contents of the
duplicateNumbers
list 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.