Skip to main content

Remove Element from Array LeetCode solution in Java | Programming blog

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 :-

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 nums.
  • In if condition check given val is not matching with current array value.
  • If val is not match with current array element, then assign nums[i] array element to current array element
    • nums [3, 2, 2, 3], val = 3
    • i = 0, temp = 0.
    • 0 is less than array length 4, so it goes in loop.
      • nums[0] != 3 | 3 != 3 | false. it does not go into if loop.
    • i = 1, temp = 0.
      • nums[1] != 3 | 2 != 3 | true. it goes into if loop
      • nums[temp] = nums[i] | nums[0] = nums[1] | assign 1st element to 0th element into nums | nums [2, 2, 2, 3]
      • temp++ | 1
    • i = 2, temp = 1, numa = [2, 2, 2, 3]
      • nums[2] != 3 | 2 != 3 | true. it goes into if loop
      • nums[temp] = nums[i] | nums[1] = nums[2] | assign 2st element to 1th element into nums | nums [2, 2, 2, 3]
      • temp++ | 2
    • i = 3, temp = 2, numa = [2, 2, 2, 3]
      • nums[3] != 3 | 3 != 3 | false. it does not goes into if loop. 
    •  i = 4, temp = 2, numa = [2, 2, 2, 3]
    • Now i become 4, so i does goes into fro loop. and we got answer.
  • It doesn't matter what you leave beyond the returned length. For example if you return 2 with nums = [2,2,3,3] or nums = [2,2,0,0], your answer will be accepted.
    (From leetcode).

Solution 2 :-

public int removeElement(int[] nums, int val) {
int i = 0;
int length = nums.length;
while (i < length) {
if (nums[i] == val) {

            // assign last array element to current array position
nums[i] = nums[length - 1];
// reduce array size by one
length--;
} else {
i++;
}
}
return length;
}

In above solution we simply assign last array element to current array index.

Explanation :-

  • nums[0, 1, 2, 2, 3, 0, 4, 2], val = 2, length = 8, i = 0
    • First two time nums[i] == val is false so it goes to else condition and increment i value.
  • nums[0, 1, 2, 2, 3, 0, 4, 2], val = 2, length = 8, i = 2
    • nums[i] == val | 2 == 2 becomes true and goes in if condition.
      • nums[i] = nums[length - 1] | nums[2] = nums[7]. So nums array last element is 2 and we are copy 2 to current index 2. and decrement length by 1.
  • nums[0, 1, 2, 2, 3, 0, 4, 2], val = 2, length = 7, i = 2
    • nums[i] == val | 2 == 2 becomes true and goes in if condition.
      • nums[i] = nums[length - 1] | nums[2] = nums[6]. So nums array last element is 4 and we are copy 4 to current index 2. and decrement length by 1. 
  • nums[0, 1, 4, 2, 3, 0, 4, 2], val = 2, length = 6, i = 2
  • nums[0, 1, 4, 2, 3, 0, 4, 2], val = 2, length = 6, i = 3
    • nums[i] == val | 2 == 2 becomes true and goes in if condition.
      • nums[i] = nums[length - 1] | nums[3] = nums[5]. So nums array last element is 0 and we are copy 0 to current index 3. and decrement length by 1.
  • nums[0, 1, 4, 0, 3, 0, 4, 2], val = 2, length = 5, i = 3
  • nums[0, 1, 4, 0, 3, 0, 4, 2], val = 2, length = 5, i = 4
  • nums[0, 1, 4, 0, 3, 0, 4, 2], val = 2, length = 5, i = 5
  • And now, i < length | 5 < 5 becoms false and return length that is our answer.

 

Happy Coding.

Other articles you may like :-

Spring Boot CRUD operations using Rest API, JPA and MySql 

Spring Boot Crud Operation with Thymeleaf + MySql + JPA 

Comments

Popular posts from this blog

Plus Minus HackerRank Solution in Java | Programming Blog

Java Solution for HackerRank Plus Minus Problem Given an array of integers, calculate the ratios of its elements that are positive , negative , and zero . Print the decimal value of each fraction on a new line with 6 places after the decimal. Example 1 : array = [1, 1, 0, -1, -1] There are N = 5 elements, two positive, two negative and one zero. Their ratios are 2/5 = 0.400000, 2/5 = 0.400000 and 1/5 = 0.200000. Results are printed as:  0.400000 0.400000 0.200000 proportion of positive values proportion of negative values proportion of zeros Example 2 : array = [-4, 3, -9, 0, 4, 1]  There are 3 positive numbers, 2 negative numbers, and 1 zero in array. Following is answer : 3/6 = 0.500000 2/6 = 0.333333 1/6 = 0.166667 Lets see solution Solution 1 import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static jav...

Flipping the Matrix HackerRank Solution in Java with Explanation

Java Solution for Flipping the Matrix | Find Highest Sum of Upper-Left Quadrant of Matrix Problem Description : Sean invented a game involving a 2n * 2n matrix where each cell of the matrix contains an integer. He can reverse any of its rows or columns any number of times. The goal of the game is to maximize the sum of the elements in the n *n submatrix located in the upper-left quadrant of the matrix. Given the initial configurations for q matrices, help Sean reverse the rows and columns of each matrix in the best possible way so that the sum of the elements in the matrix's upper-left quadrant is maximal.  Input : matrix = [[1, 2], [3, 4]] Output : 4 Input : matrix = [[112, 42, 83, 119], [56, 125, 56, 49], [15, 78, 101, 43], [62, 98, 114, 108]] Output : 119 + 114 + 56 + 125 = 414 Full Problem Description : Flipping the Matrix Problem Description   Here we can find solution using following pattern, So simply we have to find Max of same number of box like (1,1,1,1). And ...