Write a Java program to print the unique elements along with their frequency in thegiven (4 x 4) integer and character matrices.Ex:Output:Element Frequency0 11 32 28 110 2
Question
Write a Java program to print the unique elements along with their frequency in thegiven (4 x 4) integer and character matrices.Ex:Output:Element Frequency0 11 32 28 110 2
Solution
Here is a simple Java program that prints the unique elements along with their frequency in a given 4x4 integer matrix. This program uses a HashMap to store the elements and their frequencies.
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
int[][] matrix = {
{0, 1, 2, 8},
{1, 0, 2, 10},
{1, 2, 0, 10},
{2, 1, 0, 8}
};
Map<Integer, Integer> frequencyMap = new HashMap<>();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
int key = matrix[i][j];
if (frequencyMap.containsKey(key)) {
frequencyMap.put(key, frequencyMap.get(key) + 1);
} else {
frequencyMap.put(key, 1);
}
}
}
for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
System.out.println("Element: " + entry.getKey() + ", Frequency: " + entry.getValue());
}
}
}
This program first initializes a 4x4 matrix with some values. Then it creates an empty HashMap. It then iterates over the matrix, and for each element, it checks if it is already in the HashMap. If it is, it increments the frequency of that element by 1. If it is not, it adds the element to the HashMap with a frequency of 1. Finally, it prints out each element and its frequency.
You can modify this program to work with a character matrix by changing the type of the matrix and the HashMap from int to char.
Similar Questions
Compute the frequencycount of the following code fragment.for(i=0;i<n; i++)for(j=0;j<n; j++)printf(“%d”,a[i][j]);
Write a Program to print all the frequencies in a given an Array.Constraints:First line consists an integer representing the length of the array(n).Length of the array must be greater than zero or else Print Invalid Input.Second line consists all the Elements of an array.All the Elements in the array must be greaterthan zero or else print Invalid Input.Example:Input 1 : 6 1 2 2 3 3 3Output 1 : 1 - 1 2 - 2 3 - 3Input 1 : -6Output 1 : Invalid InputExplanation: NAPublic Test Cases:# INPUT EXPECTED OUTPUT1 61 2 2 3 3 31 - 12 - 23 - 32 -6Invalid Input
What does the following piece of code do?for (int i = 0; i < arr.length-1; i++){ for (int j = i+1; j < arr.length; j++) { if( (arr[i].equals(arr[j])) && (i != j) ) { System.out.println(arr[i]); } }} Print the duplicate elements in the arrayPrint the element with maximum frequency Print the unique elements in the arrayPrints the element with minimum frequnecy
TaskWrite a program that reads as many as 1,000 integer values, and counts the frequency of each value in the input.Sample outputThere are no tabs in the output.Enter as many as 1000 values, ^D to end1 3 4 6 4 3 6 10 3 5 4 3 1 6 4 3 117 values read into arrayValue Freq 1 3 3 5 4 4 5 1 6 3 10 1
Create a list of integers from user input. Write the C program to find the element that occurs with the highest frequency and also find the second-highest number in the given elements.Sample Input:512234Sample Output23
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.