Skip to content
Discussion options

You must be logged in to vote

We need to find the two numbers that appear twice in an array that should contain each number from 0 to n-1 exactly once.

Let me analyze the approaches:

Approach 1: Using a Frequency Array

  • Create an array to count occurrences of each number
  • Find which numbers appear twice
  • Time Complexity: O(n), Space Complexity: O(n)

Approach 2: Using Sum and Mathematical Properties

  • Calculate expected sum vs actual sum
  • Calculate expected sum of squares vs actual sum of squares
  • Solve the system of equations to find the two numbers
  • Time Complexity: O(n), Space Complexity: O(1)

Approach 3: Using Sorting

  • Sort the array and find duplicates
  • Time Complexity: O(n log n), Space Complexity: O(1) or O(n)

Let's…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Oct 31, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Oct 31, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants