-
-
Notifications
You must be signed in to change notification settings - Fork 147
ZA-ITP-May-2025 | Christian Mayamba | Module-Data-Groups | Week 1 #573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,17 @@ | ||
function dedupe() {} | ||
function dedupe(array) { | ||
if (array.length === 0) { | ||
return []; | ||
} | ||
|
||
const duplicates = array.filter( | ||
(item, index) => array.indexOf(item) !== index | ||
); | ||
|
||
const set2 = new Set(duplicates); | ||
return array.filter((item) => !set2.has(item)); | ||
} | ||
|
||
testVar = [1, 20, "hi", 20, 6, 9, "hey"]; | ||
console.log(dedupe(testVar)); | ||
|
||
module.exports = dedupe; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests passed, well done |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,12 +16,20 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2] | |
// Given an empty array | ||
// When passed to the dedupe function | ||
// Then it should return an empty array | ||
test.todo("given an empty array, it returns an empty array"); | ||
test("given an empty array, it returns an empty array", () => { | ||
expect(dedupe([])).toEqual([]); | ||
}); | ||
|
||
// Given an array with no duplicates | ||
// When passed to the dedupe function | ||
// Then it should return a copy of the original array | ||
test("array with no duplicates, it returns a copy of the array", () => { | ||
expect(dedupe([10, 20, 30])).toEqual([10, 20, 30]); | ||
}); | ||
|
||
// Given an array with strings or numbers | ||
// When passed to the dedupe function | ||
// Then it should remove the duplicate values, preserving the first occurence of each element | ||
test("array with strings and numbers, it should remove duplicates", () => { | ||
expect(dedupe([10, "hi", "hey", 20, "hey", 30])).toEqual([10, "hi", 20, 30]); | ||
Comment on lines
32
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this test does the same as the comment describes. The comment says "preserves the first occurence", but your test doesn't keep the first "hey". |
||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels like quite a complicated way of deduplicating an array. I'm also not quite sure it's behaving as the comments in the test file suggest. Can you give this another try? First focus on making sure you understand the requirements, then see if you can make it simpler.