Created
April 10, 2021 09:54
-
-
Save tobi-bams/5412600091334d19843600c91bb3dba5 to your computer and use it in GitHub Desktop.
A Function that removes duplicate values from a sorted Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const removeDuplicates = (sortedArray) => { | |
const filteredArray = []; | |
sortedArray.forEach((num)=> { | |
if(!filteredArray.includes(num)) { | |
filteredArray.push(num); | |
} | |
}); | |
return filteredArray.length; | |
} |
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
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 anull
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 notnull
in yourremoveDuplicates
function, something likeif (!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.