Skip to content

Instantly share code, notes, and snippets.

@Frankie-B
Forked from Amaka202/algoFridaysWeek2.js
Created June 26, 2024 11:17
Show Gist options
  • Save Frankie-B/43f396f372c415eb567f37752c5110ea to your computer and use it in GitHub Desktop.
Save Frankie-B/43f396f372c415eb567f37752c5110ea 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment