Skip to content

Instantly share code, notes, and snippets.

@Amaka202
Last active May 8, 2021 14:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Amaka202/bd8625e12e8c10561adbb07183ea6132 to your computer and use it in GitHub Desktop.
Save Amaka202/bd8625e12e8c10561adbb07183ea6132 to your computer and use it in GitHub Desktop.
Algorithms Friday Week 2 Solution
// Given an array nums, and a value val, write a function to remove
// all instances of val from the array and return the new length
const removeValInstances = (arr, val) => {
if(!arr) return 0;
if(!Array.isArray(arr)) return 0;
if(!val) return arr.length;
let newArrLength = 0;
for(let i = 0; i < arr.length; i++) {
if(arr[i] === val) continue;
newArrLength++;
}
return newArrLength;
}
removeValInstances(['i', 'e', 'd'], 'i'); //2
@meekg33k
Copy link

Hello @Amaka202, thank you for participating in Week 2 of Algorithm Fridays.

This is a really decent solution and much improved from last week. I particularly like that you handled the edge cases well. Very neat!

The logic for your solution correctly centers around looping through the array to check for elements whose values are not equal to val. Do you think there's a faster way to loop through the array?

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

@Amaka202
Copy link
Author

Amaka202 commented May 8, 2021

Hello @Amaka202, thank you for participating in Week 2 of Algorithm Fridays.

This is a really decent solution and much improved from last week. I particularly like that you handled the edge cases well. Very neat!

The logic for your solution correctly centers around looping through the array to check for elements whose values are not equal to val. Do you think there's a faster way to loop through the array?

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

🙏🏻🙏🏻

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