Skip to content
Junior–Senior168 views

Counting the number of occurrences of different array elements, including objects

Task condition

The task is to implement a function that takes an array of arbitrary values and returns an object where each key is the string representation of an array element, and the corresponding value is an object with fields value (the original element) and count (the number of its occurrences).

const mixedArray = [1, "1", { a: 1 }, { a: 1 }, 1, "test", "test"];

const countElements = () => {};

const counts = countElements(mixedArray);
console.log(counts);

// "1": { value: 1, count: 2 },
// "1": { value: "1", count: 1 },
// "{\"a\":1}": { value: { a: 1 }, count: 2 },
// "test": { value: "test", count: 2 }
Frontend tasks in the code trainer