Skip to content

Commit 12d1a5b

Browse files
committed
added code for recurring characrters
1 parent 19f22b1 commit 12d1a5b

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Recurring_char_3/recurring_char.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Finding the Most Recurring Character
2+
3+
4+
/*
5+
maxCharValue is used to store the maximum value yet encountered at the point of every iteration with the for…in loop.
6+
7+
maxChar is used to store the character with the highest value on every iteration.
8+
9+
*/
10+
function maxRecurringChar(text) {
11+
let charMap = {}
12+
let maxCharValue = 0
13+
let maxChar = ''
14+
15+
for (let char of text) {
16+
if (charMap.hasOwnProperty(char)) {
17+
charMap[char]++
18+
} else {
19+
charMap[char] = 1
20+
}
21+
}
22+
23+
for (let char in charMap) {
24+
if (charMap[char] > maxCharValue) {
25+
maxCharValue = charMap[char]
26+
maxChar = char
27+
}
28+
}
29+
30+
return maxChar
31+
}
32+
33+
console.log(maxRecurringChar('success'))
34+
// will return 's' because it is occuring 3 times

0 commit comments

Comments
 (0)