File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments