Frequency of elements of an array GFG
package com.company;
public class Array12_1 {
public static void freq(int[] arr) {
int n = arr.length;
int count = 1;
int res = 0 ;
for (int i = 1; i < n; i++) {
res = i;
if (arr[i] == arr[i - 1]) {
count++;
} else {
System.out.println(count);
count = 1;
}
}
if (n == 1 || res == n - 1) {
System.out.println(count);
}
}
public static void main(String[] args) {
int[] array = {40,50,50,50};
freq(array);
}
}
Comments
Post a Comment