Skip to content

Commit fe31b7a

Browse files
author
obada othman
committed
made changes suggested by reviewer on ex-2
1 parent 33ce73b commit fe31b7a

2 files changed

Lines changed: 12 additions & 13 deletions

File tree

Week3/js-exercises/ex2-RemoveDuplicates.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,16 @@ does not return anything but removes any duplicate elements from the array.
1313
// WRITE YOUR FUNCTION HERE
1414
// debugger;
1515
const letters = ['a', 'b', 'c', 'd', 'a', 'e', 'f', 'c', 'b'];
16-
17-
function removeDuplicates(letters) {
18-
let occuredOnce = [];
19-
for (let i = 0; i < letters.length; i++) {
20-
letter = letters[i];
21-
if (occuredOnce.indexOf(letter) === -1) {
22-
occuredOnce.push(letter);
16+
let removeDuplicates = (letters) => {
17+
letters.sort().forEach((letter, index) => {
18+
if (letters.indexOf(letter) !== index) {
19+
letters.splice(index, 1);
2320
}
24-
}
25-
26-
return occuredOnce;
27-
}
28-
21+
});
22+
return letters;
23+
};
2924
console.log(removeDuplicates(letters));
3025

31-
if (JSON.stringify(removeDuplicates(letters)) === JSON.stringify(['a', 'b', 'c', 'd', 'e', 'f']))
26+
if (JSON.stringify(removeDuplicates(letters)) === JSON.stringify(['a', 'b', 'c', 'd', 'e', 'f'])) {
3227
console.log('Hooray!');
28+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"git.ignoreLimitWarning": true
3+
}

0 commit comments

Comments
 (0)