↓Skip to main content
  1. LeetCode/

LeetCode 442: Find All Duplicates in an Array

·1 min· ·
LeetCode Medium Hash-Table Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-31

  • Submissions: Time Limit Exceeded ×2 → Accepted

Approach
#

Values are in 1..n, so nums[v - 1] can serve as the “seen” flag for v (Hash Table Template 2):

  • The first time v appears, make nums[v - 1] negative.
  • When v appears again, nums[v - 1] is already negative, so v is a duplicate.

Read values with abs, since a value may already have been negated by another number.

My Attempts
#

The first two versions checked -num in nums; in on a list is O(n), making it O(n²), TLE. Looking up by index directly passed.

Solution
#

class Solution:
    def findDuplicates(self, nums: List[int]) -> List[int]:
        res = []
        for x in nums:
            i = abs(x) - 1
            if nums[i] < 0:
                res.append(abs(x))
            else:
                nums[i] = -nums[i]
        return res

Complexity
#

Time O(n), extra space O(1) besides the output.

Mistakes to Avoid
#

  • in on a list is a linear search.
  • Read values with abs.

Interview Explanation
#

With values in 1 to n, I use the sign of nums[v - 1] to record whether v has appeared. The first visit negates it; on the second it is already negative, so v goes into the answer. Time O(n), extra space O(1).

Related

LeetCode 923: 3Sum With Multiplicity
·4 mins
LeetCode Medium Two-Pointers Sorting Hash-Table Counting Array
LeetCode 923 note: sort, fix the first number, and find the other two with converging pointers; equal end values are counted with a combination, different ones by multiplying each end’s duplicate count
LeetCode 2537: Count the Number of Good Subarrays
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2537 note: adding x creates cnt[x] new equal pairs and removing it loses cnt[x] - 1; an “at least k pairs” counting window with count += left
LeetCode 2875: Minimum Size Subarray in Infinite Array
·2 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 2875 note: strip target // sum full cycles; the remainder is smaller than one cycle, so a sliding window over two copies of nums always finds it
LeetCode 2831: Find the Longest Equal Subarray
·2 mins
LeetCode Medium Sliding-Window Hash-Table Binary-Search Array
LeetCode 2831 note: list the positions of each value and slide a window over that list; the deletions needed are the span of positions minus the number of copies
LeetCode 3795: Minimum Subarray Length With Distinct Sum At Least K
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 3795 note: a shortest-window slide where each value counts once; track each value’s last position, and subtract the left value only if it was the last occurrence
LeetCode 3679: Minimum Discards to Balance Inventory
·3 mins
LeetCode Medium Sliding-Window Hash-Table Simulation Counting Array
LeetCode 3679 note: the rule already decides what must be discarded, so simulate it; the length-w window counts only kept arrivals, and discarded ones must not be subtracted when they leave