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.
- Calculate the sum of all numbers from 0 up to the length of the array (
n). - Use a loop to calculate the actual sum of the numbers currently in the array.
- The difference between the expected sum and the actual sum is your missing number.
- Print the final result.
01EXAMPLE 1
Input
[1, 3, 4, 2, 2]Output
2Explanation: 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
JavaScriptSystem handles I/O — write your function only
Loading...
1 Hidden
Input Arguments
nums[1,3,4,2,2]
Expected Output
2
Click RUN to test your solution