Content-Length: 285300 | pFad | http://github.com/lgope/JavaScript/commit/c18df604b49321c51483f8570ce663ab3a5f6cf6

34 Find non negative duplicates and how many times · lgope/JavaScript@c18df60 · GitHub
Skip to content

Commit c18df60

Browse files
authored
Find non negative duplicates and how many times
1 parent b58c7ac commit c18df60

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Interview-Questions/findDuplicates.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Find non negative duplicates and how many times
2+
3+
let array = [1, 4, -1, 8, 2, 4, 4, 4, 1, -1, 6, 2, 1, 9, 7];
4+
function findDuplicates(arr) {
5+
let duplicateElements = {};
6+
arr.forEach((currentValue, currentIndex) => {
7+
8+
// return currentValue > 0 && arr.indexOf(currentValue) !== currentIndex; // return non negative and duplicate elements
9+
10+
// find how many times
11+
if(currentValue > 0 && arr.indexOf(currentValue) !== currentIndex){
12+
duplicateElements[currentValue] = (duplicateElements[currentValue] || 1) + 1;
13+
}
14+
15+
// console.log(currentValue, currentIndex, arr.indexOf(currentValue))
16+
});
17+
18+
19+
return duplicateElements;
20+
}
21+
22+
console.log(findDuplicates(array), Object.keys(findDuplicates(array)));

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/lgope/JavaScript/commit/c18df604b49321c51483f8570ce663ab3a5f6cf6

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy