Java Array Program · A14
Find Duplicate Elements in an Array in Java
Count how often each value occurs, then report the values with a count above one. Sorting first is the alternative that needs no map.
Problem Statement
Write a method that returns every value that occurs more than once in an int array, each value once, in ascending order.
If no value repeats, return an empty list.
Input/Output Examples
[3, 8, 3, 5, 8, 8]Output[3, 8]Explanation: 3 appears twice and 8 three times.[1, 2, 3]Output[]Explanation: every value is unique.[0, 0]Output[0]Explanation: zero is a value like any other.Visual Explanation
- Countcounts3:25:18:3TreeMap keeps keys sorted
- Filtercount > 138keep repeated values
- Done[3, 8]
[3, 8, 3, 5, 8, 8] in a TreeMap: keys come out sorted, and the ones with a count above 1 (amber) are the duplicates.Counting first means a value repeated three times is still reported only once. The sorted keys of the TreeMap give the required ascending order directly.
Approaches
Both approaches report the duplicates in ascending order. One counts in a map; the other sorts a copy.
Approach 1: TreeMap Counts (Recommended)
Idea: count every value in a TreeMap, then collect the keys with a count above 1.
- Call
counts.merge(n, 1, Integer::sum)for each value. - Iterate over the map in key order.
- Add each key whose count is above 1.
Time: O(n log n) · Space: O(n).
Approach 2: Sort a Copy, Then Scan
Idea: after sorting, equal values are neighbours; report a value when it equals its predecessor and was not just reported.
- Sort a copy so the input is not changed.
- For each
i, checksorted[i] == sorted[i - 1]. - Add the value only if it is not already the last item of the result.
Time: O(n log n) · Space: O(n). For the copy; sorting the input directly needs O(1) extra.
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: TreeMap Counts (Recommended)
The counts are available too, which helps if the interviewer asks how many times each value repeats.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class DupElementsTreeMap {
// Counts every value in a TreeMap (sorted keys), then keeps those seen more than once
static List<Integer> duplicates(int[] arr) {
Map<Integer, Integer> counts = new TreeMap<>();
for (int n : arr) {
counts.merge(n, 1, Integer::sum);
}
List<Integer> result = new ArrayList<>();
counts.forEach((value, count) -> {
if (count > 1) {
result.add(value);
}
});
return result;
}
public static void main(String[] args) {
int[][] inputs = {{3, 8, 3, 5, 8, 8}, {1, 2, 3}, {0, 0}};
for (int[] arr : inputs) {
System.out.println(Arrays.toString(arr) + " -> " + duplicates(arr));
}
}
}
[3, 8, 3, 5, 8, 8] -> [3, 8] [1, 2, 3] -> [] [0, 0] -> [0]
Time complexity: O(n log n) · Space complexity: O(n).
Approach 2: Sort a Copy, Then Scan
No map is needed, and the result is naturally in ascending order.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class DupElementsSort {
// After sorting a copy, equal values sit next to each other
static List<Integer> duplicates(int[] arr) {
int[] sorted = arr.clone();
Arrays.sort(sorted);
List<Integer> result = new ArrayList<>();
for (int i = 1; i < sorted.length; i++) {
boolean repeat = sorted[i] == sorted[i - 1];
boolean alreadyAdded = !result.isEmpty() && result.get(result.size() - 1) == sorted[i];
if (repeat && !alreadyAdded) {
result.add(sorted[i]);
}
}
return result;
}
public static void main(String[] args) {
int[][] inputs = {{3, 8, 3, 5, 8, 8}, {1, 2, 3}, {0, 0}};
for (int[] arr : inputs) {
System.out.println(Arrays.toString(arr) + " -> " + duplicates(arr));
}
}
}
[3, 8, 3, 5, 8, 8] -> [3, 8] [1, 2, 3] -> [] [0, 0] -> [0]
Time complexity: O(n log n) · Space complexity: O(n).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| TreeMap Counts | O(n log n) | O(n) |
| Sort a Copy, Then Scan | O(n log n) | O(n) |
Here n is the number of elements. Both approaches are dominated by O(n log n) ordering work. If the order does not matter, a HashMap gives O(n) time.
Interview Tips and Recommended Approach
- Report each duplicate once. Printing inside the first loop prints 8 twice for three 8s.
- Clarify the output order. Sorted, first-appearance and second-appearance orders can all differ.
- Know the bounded-range trick. If values are in
0..n-1, index marking finds duplicates inO(1)extra space. - Avoid changing the input. Sort a copy unless modifying the array is allowed.
Recommended Approach: TreeMap Counts
Why: it counts each value once, reports each duplicate once and gives sorted output without a separate sort step.