-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_occurances.js
52 lines (42 loc) · 990 Bytes
/
find_occurances.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const findTotalOccurancesEachCharInAString = (str) => {
let charMap = {};
let merge = "";
for (let char of str) {
if (charMap[char]) {
charMap[char]++;
} else {
charMap[char] = 1;
}
}
for (let char in charMap) {
merge += char + charMap[char];
}
return merge;
};
const findTotalOccurancesEachCharInAStringV2 = (str) => {
let charMap = {};
for (let char of str) {
if (charMap[char]) {
charMap[char]++;
} else {
charMap[char] = 1;
}
}
return Object.keys(charMap)
.filter((res) => charMap[res] > 1)
.join("");
};
const findTotalOccurancesEachCharInAStringV3 = (str) => {
let charMap = {};
str.split("").forEach((char) => {
charMap[char] = (charMap[char] || 0) + 1;
});
return Object.keys(charMap)
.filter((res) => charMap[res] > 1)
.join("");
};
export {
findTotalOccurancesEachCharInAString,
findTotalOccurancesEachCharInAStringV2,
findTotalOccurancesEachCharInAStringV3,
};