Skip to content

Commit cba8825

Browse files
authored
Update 442-find-all-duplicates-in-an-array.js
1 parent 70cc79c commit cba8825

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

442-find-all-duplicates-in-an-array.js

+16
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,19 @@ const findDuplicates = function(nums) {
1919

2020
console.log(findDuplicates([4, 3, 2, 7, 8, 2, 3, 1]));
2121
console.log(findDuplicates([10, 2, 5, 10, 9, 1, 1, 4, 3, 7]));
22+
23+
// another
24+
25+
/**
26+
* @param {number[]} nums
27+
* @return {number[]}
28+
*/
29+
const findDuplicates = function(nums) {
30+
const res = []
31+
for(let i = 0, len = nums.length; i < len; i++) {
32+
const idx = Math.abs(nums[i]) - 1
33+
if(nums[idx] < 0) res.push(idx + 1)
34+
nums[idx] = -nums[idx]
35+
}
36+
return res;
37+
};

0 commit comments

Comments
 (0)