-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind-all-duplicates-in-an-array.js
More file actions
38 lines (30 loc) · 961 Bytes
/
find-all-duplicates-in-an-array.js
File metadata and controls
38 lines (30 loc) · 961 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
/*
Find All Duplicates in an Array
Given an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears once or twice, return an array of all the integers that appears twice.
You must write an algorithm that runs in O(n) time and uses only constant extra space.
Example 1:
Input: nums = [4,3,2,7,8,2,3,1]
Output: [2,3]
Example 2:
Input: nums = [1,1,2]
Output: [1]
Example 3:
Input: nums = [1]
Output: []
*/
var findDuplicates = function(nums) {
let op = []
if (nums.length > 1) {
let output = {}
for(let i = 0; i < nums.length; i++) {
output[nums[i]] = output[nums[i]] ? output[nums[i]] + 1 : 1;
}
for (let [key, value] of Object.entries(output)) {
if(value > 1) { op.push(parseInt(key)) }
}
// for (let i in output) {
// if(output[i] > 1) { op.push(parseInt(i)) }
// }
}
return op;
};