Advertisement

Find All Duplicates in an Array - LeetCode 442 Solution

Find All Duplicates in an Array - Complete Solution Guide

Find All Duplicates in an Array is LeetCode problem 442, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears at most twice , return an array of all the integers that appears twice . You must write an algorithm that runs in O(n) time and uses only constant auxiliary space, excluding the space needed to store the output Example 1: Input: nums = [4,3,2,7,8,2,3,1] Output: [2,3] Example 2: Input: nums = [1,1,2] Output: [1] Example 3: Input: nums = [1] Output: [] Constraints: n == nums.leng

Detailed Explanation

The problem asks us to find all duplicate numbers in an integer array `nums` where each number is within the range [1, n], and each number appears either once or twice. We need to return an array containing the numbers that appear twice. The critical constraint is to achieve O(n) time complexity and constant auxiliary space complexity, excluding the output array's space.

Solution Approach

The solution iterates through the input array `nums`. For each number `num`, it calculates the index `index` as `abs(num) - 1`. It then checks the sign of `nums[index]`. If `nums[index]` is negative, it means that the number `abs(num)` has been encountered before, and therefore, it's a duplicate. The duplicate is added to the result array. If `nums[index]` is positive, it means the number `abs(num)` is encountered for the first time, and we negate `nums[index]` to mark it as visited. This effectively uses the array itself as a hash table to detect duplicates without requiring additional space.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty list `result` to store the duplicate numbers.
  2. Step 2: Iterate through the input array `nums` using a loop.
  3. Step 3: For each number `num` in `nums`, calculate the index `index = abs(num) - 1`.
  4. Step 4: Check the sign of `nums[index]`:
  5. - If `nums[index] < 0`, it means the number `abs(num)` is a duplicate. Add `abs(num)` to the `result` list.
  6. - If `nums[index] >= 0`, it means the number `abs(num)` is encountered for the first time. Negate `nums[index]` to mark it as visited: `nums[index] = -nums[index]`.
  7. Step 5: After iterating through all the numbers in `nums`, return the `result` list.

Key Insights

  • Insight 1: The problem leverages the fact that the numbers are within the range [1, n] to use the input array itself as a hash table. This allows us to achieve constant extra space.
  • Insight 2: By negating the number at the index corresponding to the current number's value (minus 1), we can mark that number as visited. If we encounter the same number again, the corresponding index will already have a negative value, indicating a duplicate.
  • Insight 3: The absolute value of the number is used to determine the index to avoid issues with negative indices. The sign of the number at the index indicates whether the number has been seen before.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Hash Table.

Companies

Asked at: Pocket Gems.