Skip to content

Commit d7200c9

Browse files
committed
wrote the function for dedupe and wrote test cases for it to pass the acceptance criteria
1 parent cbd8d1b commit d7200c9

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

Sprint-1/implement/dedupe.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
function dedupe() {}
1+
function dedupe(arr) {
2+
if (!Array.isArray(arr)) return null;
3+
4+
const seen = [];
5+
6+
for (const item of arr) {
7+
if (!seen.includes(item)) {
8+
seen.push(item);
9+
}
10+
}
11+
12+
return seen;
13+
}
14+
module.exports = dedupe;

Sprint-1/implement/dedupe.test.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,31 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2]
1313

1414
// Acceptance Criteria:
1515

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");
20-
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
16+
describe("dedupe", () => {
17+
// Given an empty array
18+
// When passed to the dedupe function
19+
// Then it should return an empty array
20+
test("given an empty array, it returns an empty array", () => {
21+
expect(dedupe([])).toEqual([]);
22+
});
23+
24+
// Given an array with no duplicates
25+
// When passed to the dedupe function
26+
// Then it should return a copy of the original array
27+
test("returns a copy when no duplicates exist", () => {
28+
const input = [1, 2, 3];
29+
const output = dedupe(input);
30+
31+
expect(output).toEqual([1, 2, 3]);
32+
expect(output).not.toBe(input); // ensures it's a new array, not the same reference
33+
});
34+
35+
// Given an array with strings or numbers
36+
// When passed to the dedupe function
37+
// Then it should remove the duplicate values, preserving the first occurrence
38+
test("removes duplicates and preserves first occurrence", () => {
39+
expect(dedupe(['a','a','a','b','b','c'])).toEqual(['a','b','c']);
40+
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
41+
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
42+
});
43+
});

0 commit comments

Comments
 (0)