fork download
  1. // Count Frequencies
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. int[] arr = {5, 2, 9, 1, 5, 6}; // Input array
  6.  
  7. System.out.println("Frequency of elements:");
  8.  
  9. // Traverse the array to count frequencies
  10. for (int i = 0; i < arr.length; i++) {
  11. int count = 1; // Initialize count for the current element
  12.  
  13. // Check if the element is already counted
  14. boolean isCounted = false;
  15. for (int j = 0; j < i; j++) {
  16. if (arr[i] == arr[j]) {
  17. isCounted = true; // Mark as counted
  18. break;
  19. }
  20. }
  21.  
  22. // If the element is already counted, skip to the next element
  23. if (isCounted) {
  24. continue;
  25. }
  26.  
  27. // Count the frequency of the current element
  28. for (int j = i + 1; j < arr.length; j++) {
  29. if (arr[i] == arr[j]) {
  30. count++;
  31. }
  32. }
  33.  
  34. // Print the frequency of the current element
  35. System.out.println(arr[i] + ": " + count);
  36. }
  37. }
  38. }
Success #stdin #stdout 0.18s 57544KB
stdin
Standard input is empty
stdout
Frequency of elements:
5: 2
2: 1
9: 1
1: 1
6: 1