-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmostFrequentCharacter.js
66 lines (63 loc) · 2.2 KB
/
mostFrequentCharacter.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function mostFrequentCharacter(str) {
// Create a Map to store the frequency of each character
const freq = new Map();
// Loop through the string and update the frequency of each character
for (let i = 0; i < str.length; i++) {
const char = str[i];
freq.set(char, (freq.get(char) || 0) + 1);
}
// Find the character(s) with the highest frequency
let maxFreq = 0;
let maxChars = [];
for (const [char, count] of freq) {
if (count > maxFreq) {
maxFreq = count;
maxChars = [char];
} else if (count === maxFreq) {
maxChars.push(char);
}
}
// If there is a unique most frequent character, return it
if (maxChars.length === 1) {
return maxChars[0];
} else {
return null;
}
}
console.log(mostFrequentCharacter('hello world')); // 'l'
console.log(mostFrequentCharacter('The quick brown fox jumps over the lazy dog.')); // 'o'
console.log(mostFrequentCharacter('')); // null
console.log(mostFrequentCharacter('abbccc')); // null
console.log(mostFrequentCharacter('aabbc')); // 'a' or 'b'
function mostFrequentCharacter1(str) {
// Create an object to store the frequency of each character
const freq = {};
// Loop through the string and update the frequency of each character
for (let i = 0; i < str.length; i++) {
const char = str[i];
freq[char] = (freq[char] || 0) + 1;
}
// Find the character(s) with the highest frequency
let maxFreq = 0;
let maxChars = [];
for (const char in freq) {
const count = freq[char];
if (count > maxFreq) {
maxFreq = count;
maxChars = [char];
} else if (count === maxFreq) {
maxChars.push(char);
}
}
// If there is a unique most frequent character, return it
if (maxChars.length === 1) {
return maxChars[0];
} else {
return null;
}
}
console.log(mostFrequentCharacter1('hello world')); // 'l'
console.log(mostFrequentCharacter1('The quick brown fox jumps over the lazy dog.')); // 'o'
console.log(mostFrequentCharacter1('')); // null
console.log(mostFrequentCharacter1('abbccc')); // null
console.log(mostFrequentCharacter1('aabbc')); // 'a' or 'b'