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

Java Hashset HackerRank Solution | Programming Blog

Java Hashset HackerRank Solution with Explanation   Problem Statement :- In computer science, a set is an abstract data type that can store certain values, without any particular order, and no repeated values. {1,2,3} is an example of a set, but {1,2,2} is not a set. Today you will learn how to use sets in java by solving this problem. You are given n pairs of strings. Two pairs (a,b) and (c,d) are identical if a = c and b = d. That also implies (a,b) is not same as (b,a). After taking each pair as input, you need to print number of unique pairs you currently have. See full problem description in HackerRank Website :- https://www.hackerrank.com/challenges/java-hashset/problem Let's see solution of problem. import java.util.HashSet; import java.util.Scanner; public class Solution {     public static void main(String[] args) {         Scanner s = new Scanner(System.in);         System.out.println("Enter tot...