Last active
May 8, 2021 14:33
-
-
Save Amaka202/bd8625e12e8c10561adbb07183ea6132 to your computer and use it in GitHub Desktop.
Algorithms Friday Week 2 Solution
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
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🙏🏻🙏🏻