-
Notifications
You must be signed in to change notification settings - Fork 0
/
removeDuplicate.js
63 lines (50 loc) · 2.2 KB
/
removeDuplicate.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Grandpa's hand isn't as steady as it used to be. You finally convinced him
to start using a password manager, but he accidentally typed and saved his
password with a bunch of extra characters. Help him recover his password by
removing all the duplicate characters.
Your function should take in a string of characters and return a
string with the duplicate characters removed. Assume that your input
is lowercased with only letters and numbers.
Example input: "aabbccb1212"
Example output: "abc12"
*/
function removeDupeChars(chars) {
let strWithoutDuplicates = '';
return chars.split('').filter( item => {
if (strWithoutDuplicates.indexOf(item) < 0) { // add it if not found
strWithoutDuplicates += item;
return true;
}
return false;
}).join('');
}
console.log(removeDupeChars("aabbccb1212"));
// +*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
// +*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
// +*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
/* Chef Mario's Recipe Book
Chef Mario was in the middle of writing his cookbook masterpiece
when he spilled coffee on his keyboard! Now all his recipes have repeat
ingredients.
Help save Chef Mario's cookbook by writing a function that takes in an array
and returns a new array with all the duplicates removed.
Example input: ["🌈 rainbow", "🦄 unicorn", "🍭 lollipops", "🦄 unicorn", "🍭 lollipops"];
Example output: ["🌈 rainbow", "🦄 unicorn", "🍭 lollipops"];
*/
function removeDupesFromArray(arr){
// Avoiding using nested loops: create a new object to keep track of duplicates
// use filter to loop thorugh each item in the arr
const trackDups = {};
return arr.filter(item => {
// Look up the item in the lookup table, when it doesn't exist, add it and return true
if (!trackDups[item]) {
trackDups[item] = item;
return true;
}
return false;
});
// Shorter alternative solution is using Set, which removes duplicates implicitly
// return [...new Set(arr)];
}
module.exports = { removeDupeChars, removeDupesFromArray };