136. Single Number

Description:

Given an array of integers, every element appears twice except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

My code:

/**
 * @param {number[]} nums
 * @return {number}
 */
var singleNumber = function(nums) {
    let arr = nums.sort();
    for(let i = 0; i < arr.length;) {
        if(arr[i] == arr[i + 1]) {
            i += 2;
        } else {
            return arr[i];
        }
    }
};

你可能感兴趣的:(136. Single Number)