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

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

Sales by Match HackerRank Solution | Java Solution

HackerRank Sales by Match problem solution in Java   Problem Description : Alex works at a clothing store. There is a large pile of socks that must be paired by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. For example, there are n=7 socks with colors socks = [1,2,1,2,1,3,2]. There is one pair of color 1 and one of color 2 . There are three odd socks left, one of each color. The number of pairs is 2 .   Example 1 : Input : n = 6 arr = [1, 2, 3, 4, 5, 6] Output : 0 Explanation : We have 6 socks with all different colors, So print 0. Example 2 : Input : n = 10 arr = [1, 2, 3, 4, 1, 4, 2, 7, 9, 9] Output : 4 Explanation : We have 10 socks. There is pair of color 1, 2, 4 and 9, So print 4. This problem easily solved by HashMap . Store all pair of socks one by one in Map and check if any pair is present in Map or not. If pair is present then increment ans variable by 1 ...