Counting the Occurrences/Frequency of Array Elements

Counting the occurrences / frequency of array elements

const arr = [2, 2, 5, 2, 2, 2, 4, 5, 5, 9];

function foo (array) {
let a = [],
b = [],
arr = [...array], // clone array so we don't change the original when using .sort()
prev;

arr.sort();
for (let element of arr) {
if (element !== prev) {
a.push(element);
b.push(1);
}
else ++b[b.length - 1];
prev = element;
}

return [a, b];
}

const result = foo(arr);
console.log('[' + result[0] + ']','[' + result[1] + ']')
console.log(arr)

Counting frequency of each element in array - javascript

You can use Object.entries and reduce to get the name and count whose count is greater than 3.0

Since there is a typo on 7 element i.e. "Brian,". That's why it is giving out count of Brian as 4 instead of 5

const customer = [
"Andy",
"Drew",
"Andy",
"Brian",
"Andy",
"Brian",
"Andy",
"Brian,",
"Brian",
"Drew",
"Brian",
];
const custFreq = customer.reduce(
(acc, curr) => acc.set(curr, (acc.get(curr) || 0) + 1),
new Map()
);
const result = [...custFreq.entries()].reduce((acc, [key, value]) => {
if (value >= 3.0) acc[key] = value;
return acc;
}, {});
console.log(result);

Counting the occurrences / frequency of array elements

const arr = [2, 2, 5, 2, 2, 2, 4, 5, 5, 9];

function foo (array) {
let a = [],
b = [],
arr = [...array], // clone array so we don't change the original when using .sort()
prev;

arr.sort();
for (let element of arr) {
if (element !== prev) {
a.push(element);
b.push(1);
}
else ++b[b.length - 1];
prev = element;
}

return [a, b];
}

const result = foo(arr);
console.log('[' + result[0] + ']','[' + result[1] + ']')
console.log(arr)

How to count the number of occurrences of each item in an array?

no need to use jQuery for this task — this example will build an object with the amount of occurencies of every different element in the array in O(n)

var occurrences = { };
for (var i = 0, j = arr.length; i < j; i++) {
occurrences[arr[i]] = (occurrences[arr[i]] || 0) + 1;
}

console.log(occurrences); // {ab: 3, pq: 1, mn: 2}
console.log(occurrences['mn']); // 2

Example fiddle


You could also use Array.reduce to obtain the same result and avoid a for-loop

var occurrences = arr.reduce(function(obj, item) {
obj[item] = (obj[item] || 0) + 1;
return obj;
}, {});

console.log(occurrences); // {ab: 3, pq: 1, mn: 2}
console.log(occurrences['mn']); // 2

Example fiddle

How to count certain elements in array?

Very simple:

var count = 0;
for(var i = 0; i < array.length; ++i){
if(array[i] == 2)
count++;
}

How to count the frequency of the elements in an unordered list?

If the list is sorted, you can use groupby from the itertools standard library (if it isn't, you can just sort it first, although this takes O(n lg n) time):

from itertools import groupby

a = [5, 1, 2, 2, 4, 3, 1, 2, 3, 1, 1, 5, 2]
[len(list(group)) for key, group in groupby(sorted(a))]

Output:

[4, 4, 2, 1, 2]

How to count the frequencies of elements in an array

You can create a temporary object and do simple .forEach and check if current number exists in object as key, if true plus 1 to the value, otherwise create that key, then with simple .map add all key value pairs in separate object in new array

 const x = [1, 1, 2, 2, 2, 3];

const k = {};

x.forEach(v => {

if(k[v]) {

k[v] +=1;

} else {

k[v] = 1;

}

});

const y = Object.keys(k).sort((t,c) => k[c] - k[t]).map(key => ({[key]: k[key]}));

console.log(y);


Related Topics



Leave a reply



Submit