寻找重复数

function findDuplicate(nums) {
    let map = new Map()
    for(let i = 0;i < nums.length;i++){
        let item= nums[i]
        if(map.has(item)){
            return item
        }else{
            map.set(item,1)
        }
    }
    return undefined  
}

  快慢指针

const findDuplicate = (nums) => {
    let slow = 0,
        fast = 0;
    do {
        slow = nums[slow];
        fast = nums[nums[fast]];
    } while (slow != fast);
    slow = 0;
    while (slow != fast) {
        slow = nums[slow];
        fast = nums[fast];
    }
    return slow;
};

  

posted @ 2020-07-30 11:43  671_MrSix  阅读(113)  评论(0编辑  收藏  举报