Skip to content

Commit 821e75a

Browse files
committed
Added tests and implemented solution for the implement section - dedupe.js and dedupe.test.js
1 parent 0799426 commit 821e75a

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

Sprint-1/implement/dedupe.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1-
function dedupe() {}
1+
function dedupe(arr) {
2+
if (arr.length === 0) return [];
3+
4+
const noDupsArr = [];
5+
const seenItems = new Set();
6+
7+
for (const el of arr) {
8+
if (!seenItems.has(el)) {
9+
noDupsArr.push(el);
10+
seenItems.add(el);
11+
}
12+
}
13+
return noDupsArr;
14+
}
15+
module.exports = dedupe;

Sprint-1/implement/dedupe.test.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,27 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2]
1212
*/
1313

1414
// Acceptance Criteria:
15+
describe("dedupe", () => {
16+
// Given an empty array
17+
// When passed to the dedupe function
18+
// Then it should return an empty array
19+
test("given an empty array, returns an empty array", () => {
20+
expect(dedupe([])).toEqual([]);
21+
});
1522

16-
// Given an empty array
17-
// When passed to the dedupe function
18-
// Then it should return an empty array
19-
test.todo("given an empty array, it returns an empty array");
23+
// Given an array with no duplicates
24+
// When passed to the dedupe function
25+
// Then it should return a copy of the original array
26+
test("given an array with no duplicates, returns a copy of the original array", () => {
27+
const input = [1, 65, 2, 298, 3, 729];
28+
expect(dedupe(input)).toEqual([1, 65, 2, 298, 3, 729]);
29+
});
2030

21-
// Given an array with no duplicates
22-
// When passed to the dedupe function
23-
// Then it should return a copy of the original array
24-
25-
// Given an array with strings or numbers
26-
// When passed to the dedupe function
27-
// Then it should remove the duplicate values, preserving the first occurence of each element
31+
// Given an array with strings or numbers
32+
// When passed to the dedupe function
33+
// Then it should remove the duplicate values, preserving the first occurence of each element
34+
test("given an array with duplicates, removes duplicate values preserving the first occurrence", () => {
35+
const input = ["a", "b", "a", "c", "b", "d", "e", "d", "c", "a", "b"];
36+
expect(dedupe(input)).toEqual(["a", "b", "c", "d", "e"]);
37+
});
38+
});

0 commit comments

Comments
 (0)