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 the 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
Following are the steps to remove elements from the array:
- First, input the size of the array from the user in the n variable.
- Then, create an array of string types of size ‘n’.
- Add elements to an array.
- Now, input the length value in the ‘m’ variable.
- Pass both array and length values to the display() method.
- Inside the method, first, get the length of an input array in the ‘u’ variable
- Now, iterate over the array and check if the length of an element of the array is equal to the value in variable ‘u’. If it is, then decrement the value of ‘u’.
- At last, return the value of ‘u’.
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; } }
Output
Remove Specific Elements from an Array
Write a program in Java to remove odd elements from the given array. The program should return elements without odd digits.
Input and Output Format
- Input consists of integers that correspond to n, followed by n integers.
- The output consists of elements without odd numbers.
Refer sample output for formatting specifications
Sample Input :
5
3
2
5
9
8
Sample Output :
2
8
Program to remove specific elements from an Array
Following are the steps to remove odd elements from an array:
- Input the size of the array from the user.
- Next, create an array of integers of size n.
- Now, add elements to an array.
- Pass the array to the display() method.
- Inside the method, Iterate over an array using for loop and check if the number is not odd i.e even then display it.
package com.demo3; import java.util.Scanner; public class RemoveElements { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); display(a); } public static void display(int[] a) { System.out.println("Array after removing odd elements"); for (int i = 0; i < a.length; i++) { if(a[i]%2==0){ System.out.println(a[i]); } } } }
Output
Occurrence of Element in an array
Write a program that allows the user to do the following:
- Get the value from the user; fill up the array with random values ranging from zero to the specified value.
- Get a target value from the user; this is a value that the user thinks is in the array.
- Print the number of times the user thinks the target value is in the array.
Program to find the occurrence of number in an array
Following are the steps to show the user how many times the target value is repeated in the array:
- First, get the size of the array from the user.
- Now, create an array of integers with size n.
- Add random elements to an array. Pass this array to display() method.
- Inside the method, input the target value from the user.
- Declare and initialize the count value to 0.
- Now, iterate over the array using for loop and check if the array of elements is matched with the target value, then increment count value.
- Repeat the above steps till the whole array is traversed.
- At last, print the value of count.
package com.demo3; import java.util.Scanner; public class RemoveElements { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); display(a); } public static void display(int[] a) { System.out.println("Enter value to be found"); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int count = 0; for (int i = 0; i < a.length; i++) { if(a[i]==n){ count++; } } if(count>0){ System.out.println(+count+" times "+n+" has occurred"); } } }
Output
Thus, in this way, we learned how to remove odd elements as well as remove elements on the basis of certain conditions from an array in Java.