-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainsDuplicate.js
More file actions
46 lines (36 loc) · 1004 Bytes
/
ContainsDuplicate.js
File metadata and controls
46 lines (36 loc) · 1004 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
*/
/**
* @param {number[]} nums
* @return {boolean}
*/
function sorting(arr) {
let sorted = [...arr];
let i = 0;
while (i < sorted.length) {
if (sorted[i] > sorted[i + 1]) {
let save = sorted[i];
sorted[i] = sorted[i + 1];
sorted[i + 1] = save;
i = 0;
} else {
i++;
}
}
return sorted;
}
var containsDuplicate = function(nums) {
// let sorted = sorting(nums);
// I made the function above to manually sort but leetcode didnt approve since its too slow LMAO
let sorted = nums.sort((a,b) => a - b)
for (let p = 0; p < sorted.length - 1;) {
if (sorted[p] === sorted[p + 1]) {
return true
} else {
p++
}
}
return false
};
console.log(containsDuplicate([1,2,2,2,2,2,2,3,3,4]))