Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d87a3cb

Browse files
Create findOccuranceUsingMaps.js
1 parent 2ea6fdc commit d87a3cb

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

‎Javascript/findOccuranceUsingMaps.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*Given an array of strings, names. Find the number of occurrences of every string that exists in the array.
2+
3+
4+
Use Map (named occurrence) data structure to find the number of occurrences.
5+
6+
7+
*/
8+
function findOccurrence(names) {
9+
var occurence = new Map();
10+
var nameArr = names
11+
12+
for (const word of nameArr){
13+
if(occurence.has(word)){
14+
occurence.set(word, occurence.get(word)+1)
15+
}else{
16+
occurence.set(word,1)
17+
}
18+
}
19+
20+
return occurence;
21+
}
22+
23+
24+
module.exports = findOccurrence;
25+
const arr = ['jim',"selina", "bruce", "jim"]
26+
console.log(findOccurrence(arr))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /