Skip to content

Commit 95e9ebc

Browse files
committed
occurrence
1 parent 39b6426 commit 95e9ebc

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// 17. Get Number of Occurrences of Each Letter
2+
3+
// [Method 1]: Using Object
4+
5+
{
6+
function freqCount(s) {
7+
const freq = {};
8+
for (let x of s) {
9+
freq[x] = (freq[x] || 0) + 1;
10+
}
11+
return freq;
12+
}
13+
14+
console.log(freqCount("amitkumar"));
15+
}
16+
17+
// [Method 2]: Using Map
18+
19+
{
20+
function freqCount(s) {
21+
const m = new Map();
22+
for (let x of s) {
23+
m.set(x, (m.get(x) || 0) + 1);
24+
}
25+
26+
return m;
27+
}
28+
29+
console.log(freqCount("amit"));
30+
}
31+
32+
// [Method 3]: Using Regular Expression
33+
34+
{
35+
function freqCount(s) {
36+
return (s.match(/[a-z]/gi) || []).reduce((freq, x) => {
37+
freq[x] = (freq[x] || 0) + 1;
38+
return freq;
39+
}, {});
40+
}
41+
42+
console.log(freqCount("amitamit"));
43+
}

0 commit comments

Comments
 (0)