442. Find All Duplicates in an Array
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find all elements that appear twice in an array of integers. The array has some special properties: its length is n, and all its elements are within the range [1, n]. Furthermore, each element appears either once or twice. We are required to solve this problem in O(n) time complexity and with O(1) auxiliary space, meaning we cannot use extra data structures like hash maps that scale with the input size. The output array does not count towards the space complexity limit.
Solution
Find All Duplicates in an Array in Python
class Solution:
def findDuplicates(self, nums: List[int]) -> List[int]:
result = []
# Iterate through the array
for num in nums:
# Get the value and map it to an index.
# The number `x` corresponds to index `x-1`.
# We use abs() because the value at the original position of `num`
# might have been negated in a previous step.
index = abs(num) - 1
# If the number at that index is negative, we've seen it before.
# It's a duplicate.
if nums[index] < 0:
result.append(index + 1)
else:
# Mark the number at this index as seen by negating it.
nums[index] = -nums[index]
return result
Find All Duplicates in an Array in Java
class Solution {
public List<Integer> findDuplicates(int[] nums) {
List<Integer> result = new ArrayList<>();
// Iterate through the array
for (int i = 0; i < nums.length; i++) {
// Get the value and map it to an index.
// The number `x` corresponds to index `x-1`.
// We use Math.abs because the value at nums[i] might have been negated.
int index = Math.abs(nums[i]) - 1;
// If the number at the target index is negative, it means we've already
// seen this number (abs(nums[i])), so it's a duplicate.
if (nums[index] < 0) {
result.add(index + 1);
}
// Mark the number as seen by negating the value at the target index.
nums[index] = -nums[index];
}
return result;
}
}
Find All Duplicates in an Array in C++
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
vector<int> result;
// Iterate through the array
for (int i = 0; i < nums.size(); ++i) {
// Get the value and map it to an index
// The number `x` should be at index `x-1`
int index = abs(nums[i]) - 1;
// If the number at that index is negative, it means we have seen
// this number before, so it's a duplicate.
if (nums[index] < 0) {
result.push_back(index + 1);
}
// Mark the number as seen by negating the value at that index
nums[index] = -nums[index];
}
return result;
}
};
Find All Duplicates in an Array in TypeScript
function findDuplicates(nums: number[]): number[] {
const result: number[] = [];
// Iterate through the array
for (let i = 0; i < nums.length; i++) {
// Get the value and map it to an index.
// The number `x` corresponds to index `x-1`.
// We use Math.abs because the value at nums[i] might have been negated.
const index = Math.abs(nums[i]) - 1;
// If the number at the target index is negative, it means we've already
// seen this number (Math.abs(nums[i])), so it's a duplicate.
if (nums[index] < 0) {
result.push(index + 1);
}
// Mark the number as seen by negating the value at the target index.
nums[index] = -nums[index];
}
return result;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.