Skip to content

Commit b647f3a

Browse files
committed
Dedupe Excercise
1 parent 0d9e98c commit b647f3a

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

Sprint-1/implement/dedupe.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
function dedupe() {}
1+
function dedupe(arr) {
2+
let result = [];
3+
for (let item of arr) {
4+
if (!result.includes(item)) {
5+
result.push(item);
6+
}
7+
}
8+
return result;
9+
}
10+
11+
console.log(dedupe(["a", "a", "a", "b", "b", "c"]));
12+
13+
module.exports = dedupe;

Sprint-1/implement/dedupe.test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,26 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2]
1616
// Given an empty array
1717
// When passed to the dedupe function
1818
// Then it should return an empty array
19-
test.todo("given an empty array, it returns an empty array");
19+
test("dedupe an empty array returns an empty array", () =>{
20+
const inputEmpty = [];
21+
const result = dedupe(inputEmpty);
22+
expect(result).toEqual([]);
23+
});
2024

2125
// Given an array with no duplicates
2226
// When passed to the dedupe function
2327
// Then it should return a copy of the original array
28+
test(" Given an array with no duplicates", () => {
29+
const inputNoDuplicates = ["a", "b", "c" ];
30+
const result = dedupe(inputNoDuplicates);
31+
expect(result).toEqual(["a", "b", "c"]);
32+
});
2433

2534
// Given an array with strings or numbers
2635
// When passed to the dedupe function
2736
// Then it should remove the duplicate values, preserving the first occurence of each element
37+
test(" Given an array with no duplicates", () => {
38+
const inputMix = [1,1,2,2,3,3,4,4,5,5];
39+
const result = dedupe(inputMix);
40+
expect(result).toEqual([1,2,3,4,5]);
41+
});

0 commit comments

Comments
 (0)