xxxxxxxxxx
import java.util.Arrays;
//saiyam9934
public class ElementCount {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 1, 2, 3, 1, 1};
// Sort the array to group identical elements together
Arrays.sort(array);
// Initialize variables to keep track of the current element and its count
int currentElement = array[0];
int count = 1;
// Iterate through the sorted array
for (int i = 1; i < array.length; i++) {
// If the current element is the same as the previous one, increment the count
if (array[i] == currentElement) {
count++;
} else {
// If a different element is encountered, print the count of the previous element
System.out.println(currentElement + ": " + count);
// Update the current element and reset the count for the new element
currentElement = array[i];
count = 1;
}
}
// Print the count of the last element
System.out.println(currentElement + ": " + count);
}
}