-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61a1db0
commit 7fea232
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export const findMaxChar = (text: string) => { | ||
const charMap = {}; | ||
let max = 0; | ||
let maxChar = ''; | ||
|
||
for (const char of text) { | ||
charMap[char] = charMap[char] + 1 || 1; | ||
} | ||
|
||
for (const char in charMap) { | ||
if (charMap[char] > max) { | ||
max = charMap[char]; | ||
maxChar = char; | ||
} | ||
} | ||
|
||
return maxChar; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { describe, test } from 'vitest'; | ||
|
||
import { findMaxChar } from '../maxchar'; | ||
|
||
describe('Maxchar Algorithm', () => { | ||
test('maxChar function exists', () => { | ||
expect(typeof findMaxChar).toEqual('function'); | ||
}); | ||
|
||
test('Finds the most frequently used char', () => { | ||
expect(findMaxChar('a')).toEqual('a'); | ||
expect(findMaxChar('abcdefghijklmnaaaaa')).toEqual('a'); | ||
}); | ||
|
||
test('Works with numbers in the string', () => { | ||
expect(findMaxChar('ab1c1d1e1f1g1')).toEqual('1'); | ||
}); | ||
}); |