1306. Missing Number in Array (XOR Approach)

EasyBit ManipulationBit ManipulationXORArray

Given an array containing n distinct numbers taken from the range [0, n] (so exactly one value in that range is missing), return the missing number using XOR.

Input: A JSON object {"nums": [<integers>]} of length n with distinct values in [0, n].

Output: Return the single missing number.

Examples

Example 1
Input: {"nums":[3,0,1]}
Output: 2
Explanation: Range [0,3] is missing 2.
Example 2
Input: {"nums":[0,1,2,3,5]}
Output: 4
Explanation: Range [0,5] is missing 4.

Constraints

Asked by

HCLTechAmazonBloombergMicrosoftGoogleIBM
Solve this problem in the editor →