Skip to content

Commit 47210da

Browse files
committed
--update: added code for remove duplicates from string
1 parent bdad8db commit 47210da

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
});

0 commit comments

Comments
 (0)