-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmostRepeatedChar.ts
37 lines (26 loc) · 980 Bytes
/
mostRepeatedChar.ts
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
function mostRepeatedChar(s: string[]): string | null {
//track highest char
let highestchar: string | null = null;
//track highestcount
let highestcount: number = 0
// define character dict as a hashmap with string keys and the value count
const characterDict: { [key: string]: number } = {};
for (let i = 0; i < s.length; i++) {
const char = s[i]
if(!(/[a-zA-Z]/.test(char))) continue
characterDict[char] = (characterDict[char] || 0) + 1
if(characterDict[char] > highestcount){
highestcount = characterDict[char]
highestchar = char
}
}
// return highest char
return highestchar
}
// Example function to test mostRepeatedChar
function testMostRepeatedChar() {
const testString = "aabbbccde";
const result = mostRepeatedChar(testString.split(''));
console.log(`The most repeated character in "${testString}" is "${result}"`);
}
testMostRepeatedChar();