[LeetCode] Contains Duplicate (JS)

nRecode·2020년 12월 17일
0

Algorithm

목록 보기
17/48

문제

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.

접근

객체를 만들고 이미 키 값이 있으면 즉시 return true. 모두 통과하면 return false

풀이

var containsDuplicate = function(nums) {
    let obj = {};
    for(let i of nums){
        if(!obj[i]){
            obj[i] = 1;
        }else{
            return true;
        }
    }
    return false;
};
profile
안정성, 확장성 있는 서버를 구축하고 가꾸는 개발자를 목표로 공부하고 있습니다. 🤔🤔🤔🤔 부족하기에 맞지 않는 내용이 있을 수 있습니다. 가감없이 피드백 해주시면 정말 감사하겠습니다..🙏

0개의 댓글