Given an array nums and a value val , remove all instances of that value in-place and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. The order of elements can be changed. It doesn't matter what you leave beyond the new length. See full problem description on LeetCode :- Deleting items from an array Lets see solutions :- Solution 1 :- class Solution { public int removeElement(int[] nums, int val) { int temp = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] != val) { nums[temp] = nums[i]; temp++; } } return temp; } } Input and Output :- Input nums = [3,2,2,3], val = 3 Output 2, nums = [2,2] ___________________ Input nums = [0,1,2,2,3,0,4,2], val = 2 Output 5, nums = [0,1,4,0,3] Explanation :- We have to remove given value from array. Initialize any temporary variable with 0. Loop through given array n
Welcome To Programming Tutorial. Here i share about Java Programming stuff. I share Java stuff with simple examples.