27. Remove Element_leetcode

Run Settings
LanguageJavaScript
Language Version
Run Command
var removeElement = function(nums, val) { //console.log(nums); //console.log(val); for(let i =0;i< nums.length;i++){ if(val === nums[i]){ //console.log(i) nums.splice(i, 1); i--; } } }; /* This is a simple example where the elements are integers. If you have an array of objects you would need a more sophisticated routine. This works if you only want to remove a single item. If you want to remove multiple items that match your criteria there is a glitch. As the items are removed from the array the index still increments and the next item after your matched value is skipped. The simple solution is to modify the above example to decrement the index variable so it does not skip the next item in the array. */
Editor Settings
Theme
Key bindings
Full width
Lines