The Phantom Duplicate

MediumAcc. 91.8%
+35 XP 15

The Lost Coordinate

In data systems, missing information can be catastrophic. Imagine you have a sequence of identifiers from 0 to n, but one is missing from the list. Your mission is to find that "Lost Coordinate" as efficiently as possible.

The Assignment

Your mission is to identify the missing number. Your function receives an array nums.

  1. Calculate the sum of all numbers from 0 up to the length of the array (n).
  2. Use a loop to calculate the actual sum of the numbers currently in the array.
  3. The difference between the expected sum and the actual sum is your missing number.
  4. Print the final result.

01EXAMPLE 1

Input[1, 3, 4, 2, 2]
Output2

Explanation: Actual sum is 12. Expected (1-4) is 10. Duplicate is 2.

Constraints

  • Solve in O(n) time.
  • Only use loops and basic arithmetic.
ArraysMathLogic
JavaScript
Loading...
1 Hidden

Input Arguments

nums[1,3,4,2,2]

Expected Output

2

Click RUN to test your solution