forked from rohan-paul/Awesome-JavaScript-Interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove-duplicate-from-string.js
42 lines (29 loc) · 1.26 KB
/
remove-duplicate-from-string.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Removing duplicate characters from a string.
removeDupCharFromStr = (str) => {
let result = [];
for (let i = 0; i < str.length; i++) {
if ((result.indexOf(str[i])) === -1 ) {
result.push(str[i])
}
}
return result.join('');
}
// console.log(removeDupCharFromStr("Rohaaan"));
// More compact solution to remove duplicated char from str with ES6 Set
removeDupWordsFromStr_1 = (str) => {
return [...new Set([...str])].join('');
}
console.log(removeDupWordsFromStr_1("Rohaaan"));
// Remove duplicate words from string separated by a comma and a space and return the string containing only unique words - the below solution will NOT remove duplicate characters from a string, its only for words.
removeDupWordsFromStrAlt = (str) => {
return str.split(',').filter((item, index, array) => {
return index === array.indexOf(item);
}).join(',');
}
// console.log(removeDupWordsFromStrAlt("spanner, span, spaniel, span")); // => spanner, span, spaniel
// console.log(removeDupWordsFromStrAlt("Rohaaaaan")); // => Rohaaaaan
// remove duplicates from string with the new Set method of ES6
removeDupWordsFromStr = (str) => {
return Array.from(new Set(str.split(','))).toString();
}
// console.log(removeDupWordsFromStr("spanner, span, spaniel, span"));