Last active
June 26, 2024 11:17
-
-
Save Amaka202/bd8625e12e8c10561adbb07183ea6132 to your computer and use it in GitHub Desktop.
Algorithms Friday Week 2 Solution
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
// 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 |
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
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.