Day7. Contains Duplicate(217)

作者: 前端伊始 | 来源:发表于2017-11-09 22:25 被阅读0次

    问题描述
    Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
    思路:刚开始就用了双层for循环,超时了

    /**
     * @param {number[]} nums
     * @return {boolean}
     */
    var containsDuplicate = function(nums) {
        nums.sort();
        for(var i = 0; i < nums.length-1; i++){
            if(nums[i] == nums[i+1]){
                return true;
            }
        }
        return false;
    };
    

    相关文章

      网友评论

        本文标题:Day7. Contains Duplicate(217)

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