-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontainsDuplicate.js
More file actions
36 lines (32 loc) · 884 Bytes
/
containsDuplicate.js
File metadata and controls
36 lines (32 loc) · 884 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
// 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.
// Input: nums = [1,2,3,1]
// Output: true
// var containsDuplicate = function (nums) {
// for (let i = 0; i < nums.length; i++) {
// for (let j = i + 1; j < nums.length; j++) {
// if (nums[i] === nums[j]) return true;
// }
// return false;
// }
// };
// var containsDuplicate = function (nums) {
// let memory = {};
// for (let i = 0; i < nums.length; i++) {
// if (memory[nums[i]] === undefined) {
// memory[nums[i]] = i;
// } else {
// return true;
// }
// }
// return false;
// };
var containsDuplicate = function (nums) {
nums.sort((a, b) => {
return b - a;
});
for (let i = 0; i < nums.length; i++) {
if (i > 0 && nums[i - 1] === nums[i]) return true;
}
};