frequency array code example
Example 1: Count frequency of array elements js
var arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4]
const map = arr.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());
console.info([...map.keys()])
console.info([...map.values()])
console.info([...map.entries()])
Example 2: mark occurances of elements in array cpp
int sumOfDistinct(int a[], int n){
int sum = 0;
for (int i = 0; i < n; i++) {
if (a[abs(a[i]) - 1] >= 0) {
sum += abs(a[i]);
a[abs(a[i]) - 1] *= -1;
}
}
return sum;
Example 3: frequency of ele in arr
n=int(input("ENTER ARRAY SIZE "))
arr=[]
print("Enter the array elements")
for i in range(n):
arr.append(int(input()))
x=list(dict.fromkeys(arr))
for i in x:
print("\n {} occurs {} time(s)".format(i,arr.count(i)))