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

Queen's Attack II HackerRank Solution in Java with Explanation

Queen's Attack II Problem's Solution in Java (Chessboard Problem)   Problem Description : You will be given a square chess board with one queen and a number of obstacles placed on it. Determine how many squares the queen can attack.  A queen is standing on an n * n chessboard. The chess board's rows are numbered from 1 to n, going from bottom to top. Its columns are numbered from 1 to n, going from left to right. Each square is referenced by a tuple, (r, c), describing the row r and column c, where the square is located. The queen is standing at position (r_q, c_q). In a single move, queen can attack any square in any of the eight directions The queen can move: Horizontally (left, right) Vertically (up, down) Diagonally (four directions: up-left, up-right, down-left, down-right) The queen can move any number of squares in any of these directions, but it cannot move through obstacles. Input Format : n : The size of the chessboard ( n x n ). k : The number of obstacles...

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