Skip to content

Commit 3a96f61

Browse files
cyfcyf
authored andcommitted
Implement dedupe function
1 parent f448e3e commit 3a96f61

2 files changed

Lines changed: 37 additions & 6 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 [...new Set(arr)];
3+
}
4+
5+
module.exports = dedupe;

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

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const dedupe = require("./dedupe.js");
21
/*
32
Dedupe Array
43
@@ -16,13 +15,41 @@ E.g. dedupe([1, 2, 1]) returns [1, 2]
1615
// Given an empty array
1716
// When passed to the dedupe function
1817
// Then it should return an empty array
19-
test.todo("given an empty array, it returns an empty array");
18+
//test.todo("given an empty array, it returns an empty array");
19+
20+
const dedupe = require("./dedupe");
21+
22+
// Given an empty array
23+
// When passed to the dedupe function
24+
// Then it should return an empty array
25+
test("should return an empty array when given an empty array", () => {
26+
expect(dedupe([])).toEqual([]);
27+
});
2028

2129
// Given an array with no duplicates
2230
// When passed to the dedupe function
2331
// Then it should return a copy of the original array
32+
test("should return a copy of the original array when there are no duplicates", () => {
33+
expect(dedupe([1, 2, 3, 4])).toEqual([1, 2, 3, 4]);
34+
});
35+
36+
// Given an array of strings with duplicates
37+
// When passed to the dedupe function
38+
// Then it should remove duplicate strings while preserving the first occurrence
39+
test("should remove duplicate strings while preserving the first occurrence", () => {
40+
expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]);
41+
});
42+
43+
// Given an array of numbers with duplicates
44+
// When passed to the dedupe function
45+
// Then it should remove duplicate numbers while preserving the first occurrence
46+
test("should remove duplicate numbers while preserving the first occurrence", () => {
47+
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
48+
});
2449

25-
// Given an array of strings or numbers
50+
// Given an array with one element
2651
// When passed to the dedupe function
27-
// Then it should return a new array with duplicates removed while preserving the
28-
// first occurrence of each element from the original array.
52+
// Then it should return the same array
53+
test("should return the same array when it contains only one element", () => {
54+
expect(dedupe([42])).toEqual([42]);
55+
});

0 commit comments

Comments
 (0)