728x90
[Javascript] (LeetCode) 217. Contains Duplicate (Easy)
💡 구글에 Javascript 풀이가 많이 없거나, 배운 점이 있으면 포스팅합니다.
내 풀이
/**
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function(nums) {
return nums.filter((x, i) => nums.indexOf(x) !== i).length > 0;
};
- indexOf는 결국 앞에서부터 하나씩 비교하니까 O(n)일거고, 그걸 모든 원소에 적용하면 O(n**2)일듯 ㅠ
다른 사람 풀이
var containsDuplicate = function(nums) {
return new Set(nums).size < nums.length;
};
- 오 ㅋㅋㅋㅋ 셋에 넣어서 사이즈랑 원래 길이랑 같은지 확인 ㅋㅋ 꿀팁이네욥
'Coding Test > LeetCode' 카테고리의 다른 글
[Javascript] (LeetCode) 242. Valid Anagram (Easy) (0) | 2022.07.28 |
---|---|
[Javascript] (LeetCode) 387. First Unique Character in a String (Easy) (0) | 2022.07.26 |
[Javascript] (LeetCode) 7. Reverse Integer (Medium) (0) | 2022.07.25 |
[Javascript] (LeetCode)136. Single Number (Easy) (0) | 2022.07.25 |
[Javascript] (LeetCode)122. Best Time to Buy and Sell Stock II (Medium) (0) | 2022.07.22 |
[Javascript] (LeetCode)121. Best time to buy and sell stock (Easy) (0) | 2022.07.21 |
[Javascript] (LeetCode) 26. Remove Duplicates from Sorted Array (Easy) (0) | 2022.07.20 |
[Javascript] (LeetCode) 19. Remove Nth Node From End of List (Medium) (0) | 2022.07.14 |
댓글