Answer
Find Duplicate Elements in a List
Method 1: Using HashSet (Most Efficient ā O(n))
Java
public static List<String> findDuplicates(List<String> list) {
Set<String> seen = new HashSet<>();
List<String> duplicates = new ArrayList<>();
for (String item : list) {
if (!seen.add(item)) { // add() returns false if already exists
if (!duplicates.contains(item)) { // avoid adding duplicate twice
duplicates.add(item);
}
}
}
return duplicates;
}
List<String> input = Arrays.asList("Alice", "Bob", "Alice", "Charlie", "Bob", "Dave");
System.out.println(findDuplicates(input)); // [Alice, Bob]
Method 2: Using HashMap (Get Count of Each Element)
Java
public static Map<String, Integer> getDuplicatesWithCount(List<String> list) {
Map<String, Integer> countMap = new HashMap<>();
// Count occurrences
for (String item : list) {
countMap.put(item, countMap.getOrDefault(item, 0) + 1);
}
// Filter entries with count > 1
Map<String, Integer> duplicates = new LinkedHashMap<>();
for (Map.Entry<String, Integer> entry : countMap.entrySet()) {
if (entry.getValue() > 1) {
duplicates.put(entry.getKey(), entry.getValue());
}
}
return duplicates;
}
Map<String, Integer> result = getDuplicatesWithCount(input);
// {Alice=2, Bob=2}
result.forEach((k, v) -> System.out.println(k + " appears " + v + " times"));
Method 3: Using Java 8 Streams
Java
public static List<String> findDuplicatesWithStream(List<String> list) {
Set<String> seen = new HashSet<>();
return list.stream()
.filter(item -> !seen.add(item))
.distinct()
.collect(Collectors.toList());
}
// Get count map with streams
Map<String, Long> frequencyMap = list.stream()
.collect(Collectors.groupingBy(s -> s, Collectors.counting()));
// Only duplicates (count > 1)
frequencyMap.entrySet().stream()
.filter(e -> e.getValue() > 1)
.forEach(e -> System.out.println(e.getKey() + ": " + e.getValue()));
Method 4: Using Collections.frequency()
Java
public static List<String> findDuplicatesFrequency(List<String> list) {
List<String> duplicates = new ArrayList<>();
for (String item : list) {
if (Collections.frequency(list, item) > 1 && !duplicates.contains(item)) {
duplicates.add(item);
}
}
return duplicates;
}
// Note: O(n²) ā less efficient than HashSet approach
Remove Duplicates (Bonus)
Java
// Keep only unique elements ā insertion order preserved
List<String> unique = new ArrayList<>(new LinkedHashSet<>(input));
System.out.println(unique); // [Alice, Bob, Charlie, Dave]
With Integers
Java
List<Integer> numbers = Arrays.asList(1, 2, 3, 2, 4, 3, 5, 1);
// Find duplicates
Set<Integer> seen = new HashSet<>();
Set<Integer> duplicates = new LinkedHashSet<>();
for (int n : numbers) {
if (!seen.add(n)) duplicates.add(n);
}
System.out.println(duplicates); // [2, 3, 1]
In Automation Testing
Java
// Find duplicate options in a dropdown
List<String> options = driver.findElements(By.tagName("option"))
.stream()
.map(WebElement::getText)
.collect(Collectors.toList());
Set<String> seen = new HashSet<>();
List<String> duplicates = options.stream()
.filter(o -> !seen.add(o))
.collect(Collectors.toList());
assertTrue(duplicates.isEmpty(), "Dropdown has duplicate options: " + duplicates);
