Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0884651

Browse files
Adding maxChar
1 parent 031f90f commit 0884651

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

‎maxChar/directions.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# --- Directions
2+
Given a string, return the character that is most commonly used in the string.
3+
4+
# --- Examples
5+
maxChar("abcccccccd") should return "c"
6+
maxChar("apple 1231111") should return "1"
7+
8+
9+
# --- Solutions
10+
Solution No. 1
11+
12+
function maxChar(str){
13+
const charMap = {};
14+
let max = 0;
15+
let maxChar = '';
16+
17+
for(let char of str) {
18+
if(charMap[char]) {
19+
charMap[char]++;
20+
} else {
21+
charMap[char] = 1;
22+
}
23+
}
24+
25+
for(let char in charMap){
26+
if(charMap[char] > max) {
27+
max = charMap[char];
28+
maxChar = char;
29+
}
30+
}
31+
32+
return maxChar;
33+
}

‎maxChar/maxChar.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// --- Directions
2+
// Given a string, return the character that is most
3+
// commonly used in the string.
4+
// --- Examples
5+
// maxChar("abcccccccd") === "c"
6+
// maxChar("apple 1231111") === "1"
7+
8+
function maxChar(str){
9+
const charMap = {};
10+
let max = 0;
11+
let maxChar = '';
12+
13+
for(let char of str) {
14+
if(charMap[char]) {
15+
charMap[char]++;
16+
} else {
17+
charMap[char] = 1;
18+
}
19+
}
20+
21+
for(let char in charMap){
22+
if(charMap[char] > max) {
23+
max = charMap[char];
24+
maxChar = char;
25+
}
26+
}
27+
28+
return maxChar;
29+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /