Write java program to remove all 10s from the array

Java program to remove all 10s from the array. Write a program to read an integer array and remove all 10s from the array, shift the other elements towards left and fill the trailing empty positions by 0 so that the modified array is of the same length of the given array.

The return type (Integer array) should return the final array.

Input and Output Format:

Input consists of n+1 integers, where n corresponds to size of the array followed by n elements of the array.

Output consists of an integer array (the final array).

Refer sample output for formatting specifications.

Sample Input :

5

1

10

20

10

2

Sample Output :

1

20

java program to remove all 10s from the array

package com.demo;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int i, k = 0;
int a[] = new int[n];
ArrayList<Integer> al = new ArrayList<Integer>();
for (i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
for (i = 0; i < n; i++) {
if (a[i] != 10) {
al.add(a[i]);
}
}
if (al.size() < n) {
k = n - al.size();
for (i = 0; i < k; i++) {
al.add(0);
}
}
System.out.println(al);
int b[] = new int[n];
for (i = 0; i < n; i++) {
b[i] = al.get(i);
System.out.println(b[i]);
}
}
 
}

Add a Comment

Your email address will not be published. Required fields are marked *

three × five =