File tree Expand file tree Collapse file tree 2 files changed +48
-0
lines changed
src/_Problems_/remove-duplicates Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ function removeDuplicatesUsingHashTable ( str ) {
2+ let result = '' ;
3+ const charHash = { } ;
4+
5+ // eslint-disable-next-line no-restricted-syntax
6+ for ( const char of str ) {
7+ if ( ! charHash [ char ] ) {
8+ charHash [ char ] = char ;
9+ }
10+ }
11+
12+ Object . keys ( charHash ) . map ( ( char ) => {
13+ result += char ;
14+ } ) ;
15+ return result ;
16+ }
17+
18+ function removeDuplicatesUsingSet ( str ) {
19+ return [ ...new Set ( str ) ] . join ( '' ) ;
20+ }
21+
22+ module . exports = {
23+ removeDuplicatesUsingHashTable,
24+ removeDuplicatesUsingSet,
25+ } ;
Original file line number Diff line number Diff line change 1+ const { removeDuplicatesUsingHashTable, removeDuplicatesUsingSet } = require ( '.' ) ;
2+
3+ describe ( 'Remove Duplicates' , ( ) => {
4+ describe ( 'Using Hash Table' , ( ) => {
5+ it ( 'Should remove the duplicate chars from `apple`' , ( ) => {
6+ expect ( removeDuplicatesUsingHashTable ( 'apple' ) ) . toEqual ( 'aple' ) ;
7+ } ) ;
8+
9+ it ( 'Should return `bye` from `bye`' , ( ) => {
10+ expect ( removeDuplicatesUsingHashTable ( 'bye' ) ) . toEqual ( 'bye' ) ;
11+ } ) ;
12+ } ) ;
13+
14+ describe ( 'Using ES6 Set' , ( ) => {
15+ it ( 'Should remove the duplicate chars from `apple`' , ( ) => {
16+ expect ( removeDuplicatesUsingSet ( 'apple' ) ) . toEqual ( 'aple' ) ;
17+ } ) ;
18+
19+ it ( 'Should return `bye` from `bye`' , ( ) => {
20+ expect ( removeDuplicatesUsingSet ( 'bye' ) ) . toEqual ( 'bye' ) ;
21+ } ) ;
22+ } ) ;
23+ } ) ;
You can’t perform that action at this time.
0 commit comments