My issue
You are given the following as a user input:
First line contains the integer 𝑁.
The second line contains 𝑁 space separated integers.
The third line contains an integer 𝑋.
You need to delete all occurrences of X from the list and output the new list.
Check the sample output for more details.
My code
import java.util.Scanner;
class Codechef
{
public static void main (String[] args)
{
// your code goes here
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int[] arr= new int[10];
for(int i=0;i<n;i++){
arr[i]= s.nextInt();
}
int x = s.nextInt();
for(int i = 0; i<n;i++){
if(arr[i]==x){
for(int j=i;j<n;j++){
arr[j]=arr[j+1];
}
n-=1;
//arr[i]=arr[i+1];
}
}
for(int i=0;i<n;i++){
System.out.print(arr[i]+" ");
}
}
}
Learning course: Learn Java
Problem Link: Coding problem - 2 in Java