Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3a75872

Browse files
committed
Most recurring character
1 parent 8145934 commit 3a75872

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ Once inside, run the command below:
99

1010
1. String reversal
1111
2. Vowels counter
12+
3. Most recurring character

src/maxRecurring/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// pick a solution and insert here to run the test.
2+
3+
function maxRecurringChar(text) {
4+
let charMap = {}
5+
let maxCharValue = 0
6+
let maxChar = ''
7+
for (let char of text) {
8+
if (charMap.hasOwnProperty(char)) {
9+
charMap[char]++
10+
} else {
11+
charMap[char] = 1
12+
}
13+
}
14+
for (let char in charMap) {
15+
if (charMap[char] > maxCharValue) {
16+
maxCharValue = charMap[char]
17+
maxChar = char
18+
}
19+
}
20+
return maxChar
21+
}
22+
23+
module.exports = maxRecurringChar;

src/maxRecurring/solutions.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// FOR...IN ITERATION METHOD
2+
function maxRecurringChar(text) {
3+
let charMap = {}
4+
let maxCharValue = 0
5+
let maxChar = ''
6+
for (let char of text) {
7+
if (charMap.hasOwnProperty(char)) {
8+
charMap[char]++
9+
} else {
10+
charMap[char] = 1
11+
}
12+
}
13+
for (let char in charMap) {
14+
if (charMap[char] > maxCharValue) {
15+
maxCharValue = charMap[char]
16+
maxChar = char
17+
}
18+
}
19+
return maxChar
20+
}
21+
22+
// FORMING ARRAYS FROM THE CHARACTER MAP METHOD
23+
function maxRecurringChar(text) {
24+
let charMap = {}
25+
let charArray =[]
26+
let vaulesArray = []
27+
let maxCharValue = 0
28+
29+
for (let char of text) {
30+
if (charMap.hasOwnProperty(char)) {
31+
charMap[char]++
32+
} else {
33+
charMap[char] = 1
34+
}
35+
}
36+
charArray = Object.keys(charMap)
37+
vaulesArray = Object.values(charMap)
38+
maxCharValue = Math.max(...vaulesArray)
39+
40+
return charArray[vaulesArray.indexOf(maxCharValue)]
41+
}

src/maxRecurring/test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const maxRecurringChar = require('./index');
2+
3+
test('maxRecurringChar is a function', () => {
4+
expect(typeof maxRecurringChar).toEqual('function');
5+
});
6+
7+
test('Finds the most frequently used character', () => {
8+
expect(maxRecurringChar('sisusbsnshsjsmskslstsw')).toEqual('s');
9+
});
10+
11+
test('Finds the most frequently used character even with mixed capitalization', () => {
12+
expect(maxRecurringChar('AbAdAabnmkAAAynjfaA')).toEqual('A');
13+
});
14+
15+
test('Finds the most used number as well', () => {
16+
expect(maxRecurringChar('b2n3n2m2l2k2i2o2')).toEqual('2');
17+
});

0 commit comments

Comments
 (0)