# Java Array Tutorials
# Java Array Programs
➤ Find Length of Array
➤ Different ways to Print Array
➤ Sum of Array Elements
➤ Average of Array Elements
➤ Sum of Two Arrays Elements
➤ Compare Two Arrays in Java
➤ 2nd Largest Number in Array
➤ How to Sort an Array in Java
➤ Reverse an Array in Java
➤ GCD of N Numbers in Java
➤ Linear Search in Java
➤ Binary Search in Java
➤ Copy Array in Java
➤ Merge 2 Arrays in Java
➤ Merge two sorted Arrays
➤ Largest Number in Array
➤ Smallest Number in Array
➤ Remove Duplicates
➤ Insert at Specific Position
➤ Add Element to Array
➤ Remove Element From Array
➤ Count Repeated Elements
➤ More Array Programs
Java Matrix Programs
➤ Matrix Tutorial in Java
➤ Print 2D Array in Java
➤ Print a 3×3 Matrix
➤ Sum of Matrix Elements
➤ Sum of Diagonal Elements
➤ Row Sum – Column Sum
➤ Matrix Addition in Java
➤ Matrix Subtraction in Java
➤ Transpose of a Matrix in Java
➤ Matrix Multiplication in Java
➤ Menu-driven Matrix Operations
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();
}
}
Output:-
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.");
}
}
Output:-
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<String, Integer> 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);
}
}
Output:
{Apple=2, Grapes=1, Papaya=1, Strawberry=1, Sapodilla=1, Mango=1, Pineapple=1, Orange=1, Banana=1}
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<String, Integer> but now it is a 2D array of int values therefore we will take HashMap<Integer, Integer>.
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<Integer, Integer> 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);
}
}
Output:-
{1=1, 2=3, 3=2, 7=2, 8=1}
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!