Skip to content

Commit 4ddb43b

Browse files
committed
Implement and test dedupe function
1 parent 9bb4c29 commit 4ddb43b

2 files changed

Lines changed: 17 additions & 2 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 Array.from(new Set(arr));
3+
}
4+
5+
module.exports = dedupe;

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,24 @@ 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, it returns 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, it returns a copy of the original array", () => {
27+
expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]);
28+
expect(dedupe(["a", "b", "c"])).toEqual(["a", "b", "c"]);
29+
});
2430

2531
// Given an array of strings or numbers
2632
// When passed to the dedupe function
2733
// Then it should return a new array with duplicates removed while preserving the
2834
// first occurrence of each element from the original array.
35+
test("given an array of strings or numbers, it returns a new array with duplicates removed while preserving the first occurrence of each element from the original array", () => {
36+
expect(dedupe(["a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]);
37+
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
38+
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
39+
});

0 commit comments

Comments
 (0)