Skip to content

Commit 30b22d7

Browse files
committed
feat(0217): Contains Duplicate
1 parent 6e213d8 commit 30b22d7

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

0217. Contains Duplicate.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Given an array of integers, find if the array contains any duplicates.
2+
3+
// 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.
4+
5+
// Example 1:
6+
7+
// Input: [1,2,3,1]
8+
// Output: true
9+
10+
// Example 2:
11+
12+
// Input: [1,2,3,4]
13+
// Output: false
14+
15+
// Example 3:
16+
17+
// Input: [1,1,1,3,3,4,3,2,4,2]
18+
// Output: truea
19+
20+
21+
// 1) indexOf
22+
/**
23+
* @param {number[]} nums
24+
* @return {boolean}
25+
*/
26+
const containsDuplicate = function(nums) {
27+
for (let i = 0; i < nums.length; i++) {
28+
if (nums.indexOf(nums[i]) !== i) {
29+
return true
30+
}
31+
}
32+
return false
33+
}
34+
// Runtime: 3180 ms, faster than 5.01% of JavaScript online submissions for Contains Duplicate.
35+
// Memory Usage: 37.6 MB, less than 94.12% of JavaScript online submissions for Contains Duplicate.
36+
37+
38+
// 2) Set
39+
/**
40+
* @param {number[]} nums
41+
* @return {boolean}
42+
*/
43+
const containsDuplicate = function(nums) {
44+
return [...new Set(nums)].length !== nums.length
45+
}
46+
// Runtime: 76 ms, faster than 53.55% of JavaScript online submissions for Contains Duplicate.
47+
// Memory Usage: 39.6 MB, less than 88.24% of JavaScript online submissions for Contains Duplicate.

0 commit comments

Comments
 (0)