美文网首页
136. Single Number

136. Single Number

作者: Icytail | 来源:发表于2017-11-20 11:30 被阅读0次

    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

          本文链接:https://www.haomeiwen.com/subject/hucuvxtx.html