Skip to content

Commit f710ed9

Browse files
committed
recurring_char information added
1 parent 12d1a5b commit f710ed9

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,65 @@ __Algorithmic Thinking:__ <p> From the challenge statement, we can infer that ou
232232

233233
__Code Implementation:__
234234

235+
We need to keep track of every character in the string as well as the number of times it exists.
236+
237+
The main concept we need here is character mapping. Our aim is to map characters to the number of times they exist.
238+
239+
for example: In string "success" <br>
240+
* s=3
241+
* u=1
242+
* c=2
243+
* e=1
244+
245+
*To implement this, an objet can be used.We loop through string received & add each character to a character map object as a key & the number of times it exists as a value*
246+
247+
```js
248+
let charmap = {
249+
s:3,
250+
u:1,
251+
c:2,
252+
e:1
253+
}
254+
```
255+
256+
**Let's implement it**
257+
258+
```js
259+
260+
/*
261+
maxCharValue is used to store the maximum value yet encountered at the point of every iteration with the for---in loop.
262+
263+
maxChar is used to store the character with the highest value on every iteration.
264+
265+
*/
266+
function maxRecurringChar(text) {
267+
let charMap = {}
268+
let maxCharValue = 0
269+
let maxChar = ''
270+
271+
for (let char of text) {
272+
if (charMap.hasOwnProperty(char)) {
273+
charMap[char]++
274+
} else {
275+
charMap[char] = 1
276+
}
277+
}
278+
279+
for (let char in charMap) {
280+
if (charMap[char] > maxCharValue) {
281+
maxCharValue = charMap[char]
282+
maxChar = char
283+
}
284+
}
285+
286+
return maxChar
287+
}
288+
289+
console.log(maxRecurringChar('success'))
290+
// will return 's' because it is occuring 3 times
291+
```
292+
293+
235294
<hr>
236295
<hr>
237296

0 commit comments

Comments
 (0)