File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ export const findMaxChar = ( text : string ) => {
2+ const charMap = { } ;
3+ let max = 0 ;
4+ let maxChar = '' ;
5+
6+ for ( const char of text ) {
7+ charMap [ char ] = charMap [ char ] + 1 || 1 ;
8+ }
9+
10+ for ( const char in charMap ) {
11+ if ( charMap [ char ] > max ) {
12+ max = charMap [ char ] ;
13+ maxChar = char ;
14+ }
15+ }
16+
17+ return maxChar ;
18+ } ;
Original file line number Diff line number Diff line change 1+ import { describe , test } from 'vitest' ;
2+
3+ import { findMaxChar } from '../maxchar' ;
4+
5+ describe ( 'Maxchar Algorithm' , ( ) => {
6+ test ( 'maxChar function exists' , ( ) => {
7+ expect ( typeof findMaxChar ) . toEqual ( 'function' ) ;
8+ } ) ;
9+
10+ test ( 'Finds the most frequently used char' , ( ) => {
11+ expect ( findMaxChar ( 'a' ) ) . toEqual ( 'a' ) ;
12+ expect ( findMaxChar ( 'abcdefghijklmnaaaaa' ) ) . toEqual ( 'a' ) ;
13+ } ) ;
14+
15+ test ( 'Works with numbers in the string' , ( ) => {
16+ expect ( findMaxChar ( 'ab1c1d1e1f1g1' ) ) . toEqual ( '1' ) ;
17+ } ) ;
18+ } ) ;
You can’t perform that action at this time.
0 commit comments