Problem · Array
Find the Missing Number from 1 to n
Learn this problemProblem statement
You are given an integer array nums of length n - 1. The array contains distinct values chosen from every integer from 1 through n, inclusive, with exactly one value missing.
Return the missing value.
Function
findMissingNumber(nums: int[]) → intExamples
Example 1
nums = [1,2,4,5]return = 3Here n = 5. The only value from 1 through 5 absent from the array is 3.
Example 2
nums = [2,3,1]return = 4The array length is 3, so n = 4; the missing value is 4.
Example 3
nums = []return = 1An empty array represents n = 1, so 1 is missing.
Constraints
1 <= n <= 100000.nums.length = n - 1.- Every element of
numsis a distinct integer in the inclusive range[1, n]. - Exactly one integer from
1throughnis absent.