Remove Element - Java

Problem : https://leetcode.com/problems/remove-element/description/ 


import java.util.Arrays;

public class test {

public static void main(String[] args) {

int nums[] = {0,1,2,2,3,0,4,2};
int val = 2;
int i=0;

for(int j =0;j<nums.length;j++){
if(nums[j]!=val){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
i++;
}
}
System.out.println(i);
System.out.println(Arrays.toString(nums));

}
}

Comments

Popular posts from this blog

Rest Assured

Sort (Bubble) - Java

Rotate the String from position - Java