Last active
April 19, 2021 19:02
-
-
Save osahondev/8d14387f1d2c031b364c823cc6db2c4b to your computer and use it in GitHub Desktop.
Function to remove instances from a value and return new length
This file contains hidden or 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 elementRemoval = (nums,val) =>{ | |
| if( nums == undefined ) | |
| return 0 | |
| return nums.filter( num=>num!=val ).length; | |
| } |
Author
Thanks for the feedback. On a second look at other implementations, perhaps there truly is a better way. I would study them to improve on my efficiency. Keep up the good work by the way. Really love Algo Friday.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @enaiho, thank you for participating in Week 2 of Algorithm Fridays.
This is a decent solution and I like that you included an edge case check to see if
numsisundefined. Your check however won't take care of cases wherenumsisnull. So a more robust way to handle this would be to take advantage of something called falsy in JavaScript.The code snippet would be something like this:
if (!nums) { return 0 };Also, I like that you used the
filterfunction to solve this problem but what do you think are the trade-offs with using thefilterfunction? Is that the most optimal solution for this problem?I've posted my solution here. Do let me know what you think.