LeetCode 442 (medium) | Find All Duplicates in an Array | O(n) Time, O(1) Space | Best Explanation

Опубликовано: 09 Август 2026
на канале: Random CPP Guy
7
1

In this video, we solve LeetCode 442 - Find All Duplicates in an Array using an O(n) time and O(1) space approach (excluding output storage).

We explain both the intuition and implementation clearly — step by step, including how to use the index marking trick to track duplicates without using extra space.

Problem Statement:
Given an array nums of length n where each element is in the range [1, n] and appears at most twice, return all numbers that appear exactly twice.

Approach:

Iterate through the array

For each element x, mark nums[abs(x)-1] negative

If already negative → it’s a duplicate

Collect duplicates in the result

Complexity:

Time: O(n)

Space: O(1) (excluding output)
#LeetCode442
#FindAllDuplicatesInAnArray
#LeetCode
#DSA
#CodingInterview
#DataStructures
#Algorithms
#CPlusPlus
#CodingTutorial
#CompetitiveProgramming
#LeetCodeSolution
#LeetCodeMedium
#CodingPractice
#InterviewPreparation
#ArrayProblems
#Programming
#Coding
#CodeWithMe
#ProblemSolving
#LearnToCode
#SoftwareEngineering
#TechInterview
#CodeExplanation
#OofN
#CodingJourney