forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
33 lines (27 loc) · 619 Bytes
/
index.js
File metadata and controls
33 lines (27 loc) · 619 Bytes
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
function findMaxchar(str) {
if (typeof str !== 'string') {
throw new Error('Invalid Argument');
}
const charMap = {};
const lowerCasedString = str.toLowerCase();
lowerCasedString.split('').forEach((char) => {
if (!charMap[char]) {
charMap[char] = 1;
} else {
charMap[char] += 1;
}
});
// find the char with highest score
let max = charMap[lowerCasedString[0]];
let char = lowerCasedString[0];
Object.keys(charMap).forEach((e) => {
if (charMap[e] > max) {
max = charMap[e];
char = e;
}
});
return char;
}
module.exports = {
findMaxchar,
};