Skip to content

Commit 8bac04f

Browse files
committed
dedupe function created & test cases created
1 parent c49c63c commit 8bac04f

2 files changed

Lines changed: 49 additions & 21 deletions

File tree

Sprint-1/implement/dedupe.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
function dedupe() {}
1+
function dedupe(arr) {
2+
if (arr.length === 0) return [];
3+
return [...new Set(arr)];
4+
}
5+
6+
module.exports = dedupe;

Sprint-1/implement/dedupe.test.js

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,50 @@
1-
const dedupe = require("./dedupe.js");
2-
/*
3-
Dedupe Array
1+
const dedupe = require("./dedupe");
42

5-
📖 Dedupe means **deduplicate**
3+
describe("dedupe", () => {
4+
// Given an empty array
5+
// When passed to the dedupe function
6+
// Then it should return an empty array
7+
test.todo("given an empty array, it returns an empty array");
8+
test("given an empty array, it returns an empty array", () => {
9+
const result = dedupe([]);
10+
expect(result).toEqual([]);
11+
});
612

7-
In this kata, you will need to deduplicate the elements of an array
13+
// Given an array with no duplicates
14+
// When passed to the dedupe function
15+
// Then it should return a copy of the original array
16+
test("given an array with no duplicates, it returns a copy of the original array", () => {
17+
const input = [1, 2, 3, 4];
18+
const result = dedupe(input);
819

9-
E.g. dedupe(['a','a','a','b','b','c']) target output: ['a','b','c']
10-
E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) target output: [5, 1, 2, 3, 8]
11-
E.g. dedupe([1, 2, 1]) target output: [1, 2]
12-
*/
20+
expect(result).toEqual([1, 2, 3, 4]);
21+
expect(result).not.toBe(input); // should be a new copy
22+
});
1323

14-
// Acceptance Criteria:
24+
// Given an array with strings
25+
// When passed to the dedupe function
26+
// Then it should remove duplicates preserving first occurrence
27+
test("removes duplicate strings preserving first occurrence", () => {
28+
const input = ["a", "a", "a", "b", "b", "c"];
29+
const result = dedupe(input);
1530

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");
31+
expect(result).toEqual(["a", "b", "c"]);
32+
});
2033

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
34+
// Given an array with numbers
35+
// When passed to the dedupe function
36+
// Then it should remove the duplicate values, preserving the first occurence of each element
37+
test("removes duplicate numbers preserving first occurrence", () => {
38+
const input = [5, 1, 1, 2, 3, 2, 5, 8];
39+
const result = dedupe(input);
2440

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
41+
expect(result).toEqual([5, 1, 2, 3, 8]);
42+
});
43+
44+
test("removes duplicates in mixed order", () => {
45+
const input = [1, 2, 1];
46+
const result = dedupe(input);
47+
48+
expect(result).toEqual([1, 2]);
49+
});
50+
});

0 commit comments

Comments
 (0)