Skip to content

Commit 1fff256

Browse files
committed
Implement dedupe with tests
1 parent 8e213b2 commit 1fff256

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

‎Sprint-1/implement/dedupe.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
function dedupe() {}
1+
function dedupe(arr) {
2+
return arr.filter((item, index) => arr.indexOf(item) === index);
3+
}
4+
5+
module.exports = dedupe;

‎Sprint-1/implement/dedupe.test.js‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,33 @@ E.g. dedupe([1, 2, 1]) returns [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("Given an empty array, should return an empty array", () => {
20+
expect(dedupe([])).toEqual([]);
21+
});
2022

2123
// Given an array with no duplicates
2224
// When passed to the dedupe function
2325
// Then it should return a copy of the original array
26+
test("Given an array with no duplicates, should return a copy of the original array", () => {
27+
expect([5, 1, 2, 3, 8]).toEqual([5, 1, 2, 3, 8]);
28+
});
2429

2530
// Given an array of strings or numbers
2631
// When passed to the dedupe function
27-
// Then it should return a new array with duplicates removed while preserving the
32+
// Then it should return a new array with duplicates removed while preserving the
2833
// first occurrence of each element from the original array.
34+
test("Given an array of strings, removes duplicates and keeps first occurrence", () => {
35+
expect(dedupe(["car", "plane", "car", "train", "plane"])).toEqual([
36+
"car",
37+
"plane",
38+
"train",
39+
]);
40+
});
41+
42+
test("Given an array of numbers, removes duplicates and keeps first occurrence", () => {
43+
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
44+
});
45+
46+
test("Given [1, 2, 1], returns [1, 2]", () => {
47+
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
48+
});

0 commit comments

Comments
 (0)