Write a java Program to Remove Elements from the array

Write a program to remove all the elements of the given length and return the size of the final array as output. If there is no element of the given length, return the size of the same array as output.

Assume maximum length of the array is 20.

Input and Output Format:

Input consists of integers that correspond to n, followed by n strings and finally m which corresponds to the length value.

The output consists of a single Integer.

Refer sample output for formatting specifications.

Sample Input 1:

5

a

bb

b

ccc

ddd

2

Sample Output 1:

4

Remove Elements from the array in Java.

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = Integer.parseInt(sc.nextLine());
    String[] a = new String[n];
    for (int i = 0; i < n; i++)
      a[i] = sc.nextLine();
    int m = Integer.parseInt(sc.nextLine());
    System.out.println(display(a, m));
  }

  public static int display(String[] a, int m) {

    int u = a.length;
    for (int i = 0; i < a.length; i++) {
      if (a[i].length() == m)
        u--;
    }
    return u;
  }

}

Add a Comment

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

nineteen − 4 =