Skip to content

Commit 5c3214a

Browse files
committed
implement the dedupe function and tests
1 parent 8a4b703 commit 5c3214a

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

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(elements) {
2+
const unique = [];
3+
4+
for (let i = 0; i < elements.length; i++) {
5+
const current = elements[i];
6+
7+
if (!unique.includes(current)) {
8+
unique.push(current);
9+
}
10+
}
11+
12+
return unique;
13+
}
14+
15+
module.exports = dedupe;

Sprint-1/implement/dedupe.test.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,30 @@ E.g. dedupe([1, 2, 1]) target output: [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");
2019

20+
test("given an empty array, it returns an empty array", () => {
21+
const currentOutput = dedupe([]);
22+
const targetOutput = [];
23+
24+
expect(currentOutput).toEqual(targetOutput);
25+
});
2126
// Given an array with no duplicates
2227
// When passed to the dedupe function
2328
// Then it should return a copy of the original array
2429

30+
test("Given an array with no duplicates, it returns an the same array", () => {
31+
const currentOutput = dedupe([1,4,6,"d","a","x","e",0,7,8]);
32+
const targetOutput = [1,4,6,"d","a","x","e",0,7,8];
33+
34+
expect(currentOutput).toEqual(targetOutput);
35+
});
2536
// Given an array with strings or numbers
2637
// When passed to the dedupe function
27-
// Then it should remove the duplicate values, preserving the first occurence of each element
38+
// Then it should remove the duplicate values, preserving the first occurrence of each element
39+
40+
test("Given an array with strings or numbers, it returns an the array whit no duplicated elements", () => {
41+
const currentOutput = dedupe(['a','a','a','b','b','c']);
42+
const targetOutput = ['a','b','c'];
43+
44+
expect(currentOutput).toEqual(targetOutput);
45+
});

0 commit comments

Comments
 (0)