Skip to content

Instantly share code, notes, and snippets.

@osahondev
Last active April 19, 2021 19:02
Show Gist options
  • Select an option

  • Save osahondev/8d14387f1d2c031b364c823cc6db2c4b to your computer and use it in GitHub Desktop.

Select an option

Save osahondev/8d14387f1d2c031b364c823cc6db2c4b to your computer and use it in GitHub Desktop.
Function to remove instances from a value and return new length
const elementRemoval = (nums,val) =>{
if( nums == undefined )
return 0
return nums.filter( num=>num!=val ).length;
}
@meekg33k
Copy link

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 nums is undefined. Your check however won't take care of cases where nums is null. 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 filter function to solve this problem but what do you think are the trade-offs with using the filter function? Is that the most optimal solution for this problem?

I've posted my solution here. Do let me know what you think.

@osahondev
Copy link
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