Skip to content

Instantly share code, notes, and snippets.

@tobi-bams
Created April 10, 2021 09:54
Show Gist options
  • Save tobi-bams/5412600091334d19843600c91bb3dba5 to your computer and use it in GitHub Desktop.
Save tobi-bams/5412600091334d19843600c91bb3dba5 to your computer and use it in GitHub Desktop.
A Function that removes duplicate values from a sorted Array
const removeDuplicates = (sortedArray) => {
const filteredArray = [];
sortedArray.forEach((num)=> {
if(!filteredArray.includes(num)) {
filteredArray.push(num);
}
});
return filteredArray.length;
}
@meekg33k
Copy link

Hello @tobi-bams, thank you for participating in Week 1 of Algorithm Fridays.

Your solution works and passes most of the test cases. The only assumption you have made is that the input array cannot have a null value because if I pass a null value to your function, it would break on line 3.

Ideally, you always want to write code that is robust and takes care of edge cases and unexpected input. Basically adding a check to see if sortedArray is not null in your removeDuplicates function, something like if (!sortedArray) return 0.

Also, is there a way we could have solved this problem without having to create another array like you did on line 2? What do you think?

I have posted my solution here, please let me know what you think.

@tobi-bams
Copy link
Author

Thank you so much @meekg33k, i really appreciate the feedback.
I have gone through the solutions and have seen what i could have done better.

I hope to do better this week and make sure i consider edge cases when writing code.

I really appreciate and thanks for organizing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment