Skip to content

Commit

Permalink
feat: maxchar algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
francislagares committed Aug 14, 2023
1 parent 61a1db0 commit 7fea232
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/algorithms/maxchar/maxchar.ts
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;
};
18 changes: 18 additions & 0 deletions src/algorithms/maxchar/tests/maxchar.test.ts
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');
});
});

0 comments on commit 7fea232

Please sign in to comment.