- Java Count Occurrences in 2D Array
- Java Count Occurrences in 2D Array Code
- Java Count Occurrences in 2D Array of Each Element
- Java Program to find the Number of Elements in an Array
- Program 1: Calculate the Number of Elements present in the Array
- Algorithm
- Program 2: Calculate the Number of Elements present in the Array
- Algorithm
- how to count element in array
- Recommended Answers Collapse Answers
Java Count Occurrences in 2D Array
Java Count Occurrences in 2D Array | In this section, we will count the occurrences of the element in the two-dimensional array that is we count the number of occurrences in the matrix. To display the matrix we use Arrays.deepToString() method this method is used for converting multidimensional arrays to strings.
To know more about the problem observe the below examples. Examples:-
1) Array = < < 1, 2 >, < 1, 5 >, < 2, 1 >>;
Element to find occurance = 1
Occurance of 1 = 3 times
2) Array = < < 1, 2 >, < 1, 5 >, < 2, 1 >>;
Element to find occurance = 2
Occurance of 2 = 2 times
3) Array = < < 1, 2 >, < 1, 5 >, < 2, 1 >>;
Element to find occurance = 5
Occurance of 5 = 1 time
4) Array = < < 1, 2 >, < 1, 5 >, < 2, 1 >>;
Element to find occurance = 0
Occurance of 0 = 0 times
5) Example to find occurrence of each element in the given 2D array:-
Array = < < 1, 2 >, < 1, 5 >, < 2, 1 >>;
Occurance of 1 = 3 times
Occurance of 2 = 2 times
Occurance of 5 = 1 time
Occurance of 0 = 0 times
Java Count Occurrences in 2D Array Code
Now let us see a Java program to count the occurrence of given element in the given 2D array.
import java.util.Arrays; import java.util.Scanner; public class Main < public static int findOccurrences(int arr[][], int element) < int count = 0; for (int i = 0; i < arr.length; i++) < for (int j = 0; j < arr[i].length; j++) < if (element == arr[i][j]) count++; >> return count; > public static void main(String args[]) < Scanner scan = new Scanner(System.in); int array[][] = < < 1, 2, 3 >, < 2, 2, 3 >, < 7, 7, 8 >>; System.out.println("Array = " + Arrays.deepToString(array)); System.out.print("Enter element to find occurrence: "); int element = scan.nextInt(); System.out.println("Occurrence of " + element + " = " + findOccurrences(array, element) + " times."); scan.close(); > >
Array = [ [1, 2, 3], [2, 2, 3], [7, 7, 8] ]Enter element to find occurrence: 2
Occurrence of 2 = 3 times.
Array = [ [1, 2, 3], [2, 2, 3], [7, 7, 8] ]Enter element to find occurrence: 8
Occurrence of 8 = 1 times.
Array = [ [1, 2, 3], [2, 2, 3], [7, 7, 8] ]Enter element to find occurrence: 9
Occurrence of 9 = 0 times.
Let us see another example for a 2D array of strings.
import java.util.Arrays; public class Main < public static int findOccurrences(String arr[][], String element) < int count = 0; for (int i = 0; i < arr.length; i++) < for (int j = 0; j < arr[i].length; j++) < if (element.equals(arr[i][j])) < count++; >> > return count; > public static void main(String args[]) < String[][] array = < < "Apple", "Banana", "Grapes", "Orange", "Strawberry" >, < "Apple", "Pineapple", "Mango", "Papaya", "Sapodilla" >>; System.out.println("Array = " + Arrays.deepToString(array)); String element = "Apple"; System.out.println("Occurrence of " + element + " = " + findOccurrences(array, element) + " times."); > >
Array = [ [Apple, Banana, Grapes, Orange, Strawberry], [Apple, Pineapple, Mango, Papaya, Sapodilla] ]Occurrence of Apple = 2 times.
Java Count Occurrences in 2D Array of Each Element
Let us see a Java program to count the occurrences of all unique elements present in the given array. To solve this problem, we will take the help of HashMap. In the HashMap, the array element will be stored as the key, and their count will be stored as the value. Finally, we will display the key, value pair of HashMap.
import java.util.HashMap; import java.util.Map; public class Main < public static void main(String args[]) < String[][] array = < < "Apple", "Banana", "Grapes", "Orange", "Strawberry" >, < "Apple", "Pineapple", "Mango", "Papaya", "Sapodilla" >>; Map count = new HashMap<>(); for (String[] arr : array) < for (String string : arr) < if (!count.containsKey(string)) < count.put(string, 1); >else < count.put(string, count.get(string) + 1); >> > System.out.println(count); > >
Another example of Java count occurrences in 2D array of integers. In the previous example, it is a 2D array of strings hence we took HashMap but now it is a 2D array of int values therefore we will take HashMap.
import java.util.HashMap; import java.util.Map; public class Main < public static void main(String args[]) < int array[][] = < < 1, 2, 3 >, < 2, 2, 3 >, < 7, 7, 8 >>; Map count = new HashMap<>(); for (int[] arr : array) < for (int element : arr) < if (!count.containsKey(element)) < count.put(element, 1); >else < count.put(element, count.get(element) + 1); >> > System.out.println(count); > >
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
Java Program to find the Number of Elements in an Array
In this tutorial, we will learn how to find the total number of elements present in an array. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.
Input: The Array Elements are: 9 8 7 0 6 5 4 7 3 4 5 2 1
Output: The total number of elements in the array is 13
Program 1: Calculate the Number of Elements present in the Array
In this method, we will see how to calculate the number of elements present in an array using a for each loop.
Algorithm
- Start
- Declare an array.
- Initialize the array.
- Declare a variable count to store the number of elements in the array.
- Initialize it to 0.
- Use a for each loop to iterate through all the elements in an array.
- Increment the count variable in each iteration.
- Print the total number of elements in the array.
- Now, print the array elements.
- Stop.
The below program demonstrates how to calculate the total number of elements in the array using a for each loop. Firstly, we declare an array and then we use a for each loop to determine the total number of elements present in the array.
/*Java Program to find the number of elements present in an array*/ import java.util.*; import java.util.Arrays; //Driver Code public class Main < public static void main(String args[]) < int a[] = ; //Declare and Initialize an array int count=0; //Declare variable to count the number of elements in an array and initialize it to 0 //Use a for each loop to iterate through all the elements in an array //Print the elements present in the array System.out.println("The entered elements are: "); for(int i:a) < System.out.print(a[i]+" "); count++; //Increment the count variable >System.out.println(""); //Print the total number of elements present System.out.println("The total number of elements in an array is "+count); > >
The entered elements are:
4 3 7 9 4 5 6 7 0 6 5 7 8
The total number of elements in an array is 13
Program 2: Calculate the Number of Elements present in the Array
In this method, we will see how to calculate the number of elements present in an array using an in-built function. Java provides an in-built function length() that returns the total length of the array. The total length of the array is nothing but the total number of elements present in the array.
Algorithm
- Start
- Declare an array.
- Initialize the array.
- Declare a variable count to store the number of elements in the array.
- Use the in-built function to calculate the length of the array.
- Print the length of the array.
- Using a for loop traverse through all the elements.
- Print all the array elements.
- Stop.
Below is the code for the same.
The below program demonstrates how to calculate the total number of elements in the array using a try-catch block. Firstly, we declare and initialize the array and then use the in-built function to determine the total number of elements present in the array.
/*Java Program to find the number of elements present in an Array using in-built functions*/ public class Main < //Driver Code public static void main(String[] arr) < int a[] = ; //Declare and Initialize an array //Declare a variable to store the length of the array int count=a.length; //Use an in-built function to calculate the length of an array System.out.println("The number of elements in the array are : "+count); //Print the length of the array //Print the array elements System.out.println("The Array Elements are "); for(int j=0;j System.out.println(""); > >
The number of elements in the array are : 12
The Array Elements are
91 28 47 30 56 65 74 87 93 24 15 82
how to count element in array
how can i count the elements in array example if I input this..10 1 1 1 2 2 5 5 5 6 the output should this it will count the elements if how many times this inputed 10= 1
1=3
1= 3
1=3
2=2
2=2
5=3
5=3
5=3
6=1
please help me. hoping for your positive responds. thank you in advance.
- 6 Contributors
- 16 Replies
- 5K Views
- 1 Day Discussion Span
- Latest Post 13 Years Ago Latest Post by jemz
Recommended Answers Collapse Answers
You need to use another collection of some type to accumulate the counts for each of the elements in the array.
One class to use could be a Hashtable with the element as the Key and the value as the count.For a small range of integers, you could …
Sorry, we don’t do homework. Pick one of the solutions and try to program it.
# # if (num[i].length == find) # < # count++; # ># # System.out.print("The elements are: " + num[i]); # System.out.print( num[i] + " btn btn-primary" href="/programming/software-development/threads/294821/how-to-count-element-in-array#post1268686">Jump to Post