count frequency of each element
in an array
[ for...of loop | Object ]

Count frequency of each element

Write a function that takes an array and returns an object with each unique element of the array as the key and the number of times each element appears in the array, as the value


Example ...

Enter an array ...

[2, 3, 6, 2, 4, 6, 2, 2, 4, 6] original array

Returns an object with each unique element of the array as the key

and the number of times each element appears in the array as the value

{"2": 4, "3": 1, "4": 2, "6": 3} object

Each key is a string.

Frequency of elements found.

"2" found 4 times

"3" found 1 time

"4" found 2 times

"6" found 3 times

Arrays are used to store multiple values in a single variable.

Each value is called an element, and each element has a numeric position in the array, known as its index.

Arrays are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.

Arrays can contain any data type, including numbers, strings, and objects.

const arr1 = [2, 4, 6]; array

arr1[0]; element at index 0 → 2

arr1[1]; element at index 1 → 4

arr1[2]; element at index 2 → 6

arr1[3]; element at index 3 → undefined index not found


Objects are a data structure used to store related data collections.

It stores data as key/value pairs, where each key is a unique identifier for the associated value

Each key must be a string and must be unique, each value can be any data type.

If you define an object with duplicate keys, the last one will overwrite any preceding ones.

Find the value for any given key in the object.

const obj1 = {"A": 4, "B": 5, "C": 6 }; object

obj1["A"]; key "A"4

obj1["B"]; key "B"5

obj1["C"]; key "C"6

obj1["D"]; key "D"undefined key not found

Find the value for any given key in the object.

const obj2 = {"A": 4, "B": 5, "C": 6 }; object

const str = "ABC"; string

obj2[str[0]];4

obj2[str[1]];5

obj2[str[2]];6

obj2[str[3]];undefined key not found


Strings are a sequence of zero or more characters written inside quotes used to represent text.

Strings may consist of letters, numbers, symbols, words, or sentences.

Strings are immutable, they cannot be changed.

Each character in a string has an index.

The first character will be index 0 the second character will be index 1 and so on.

There are two ways to access an individual character in a string.

charAt() method

const str1 = "abc"; string

str1.charAt(0); character at index 0 → "a"

str1.charAt(1); character at index 1 → "b"

str1.charAt(2); character at index 2 → "c"

str1.charAt(3); character at index 3 → "" index not found

Alternatively use at() or slice() methods

bracket notation []

const str2 = "abc"; string

str2[0]; character at index 0 → "a"

str2[1]; character at index 1 → "b"

str2[2]; character at index 2 → "c"

str2[3]; character at index 3 → undefined index not found


Numbers are used to represent both integer and floating-point values.

Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴

let num1 = 5; → number

let num2 = 2.5; → number

let num3 = num1 + num2;

console.log(num3); returns ↴

7.5 → number


Count frequency of each element in an array using a for...of loop.


for...of loop iterates through the values of an iterable object, such as an Array, String, Set, Map, ...

syntax

for (variable of iterable) {}

variable holds the current value of the iteration.

of keyword indicates that the loop should iterate over the values of the iterable.

iterable object that is iterable, such as an Array, String, Set, Map, ...

Iterate over each character in the string.

const str3 = "ABC";

for (const char of str3) {

console.log(char);

} returns ↴

A

B

C → printed to console

The loop will run three times, once for each character in the string.

On each iteration, the value of the current element is stored in the variable char

For each iteration of the loop, the current value of char is printed to the console.


Initialize an array to count frequency of each element.

const array1 = ["A", "C", "B", "A", "D", "A", "B", "C", "B", "A"]; → user input


Define a function frequency to count the frequency of each element in the array.

function frequency(arr) {}

The function takes an array as input arr and returns an object representing the frequency of each element. The original array remains unchanged.

The keys represent the unique elements from the array.

The values denote the number of times each element appears.

Initialize an empty object to hold the frequency count.

const count = {} count

Iterate over each element in arr

for (const element of arr) {}

Increment the character count in the object.

count[element] = count[element] + 1

If the element is already a key in the count object.

count[element] = count[element] + 1 → increment the count by 1

If the element is NOT found in the object, add it to the object.

count[element] = 0 + 1 → initialize it to 0 and then add 1

Logical OR || operator returns the first truthy value it encounters.

If the left operand exists, it is truthy, it evaluates and returns the left operand.

If the left operand is falsy (like undefined, null, or 0), it evaluates and returns the right operand.

count[element] attempts to retrieve the value associated with element from the count object.

If it exists, the operand is truthy and evaluates the left operand.

count[element] = count[element] + 1

If it does NOT exist, the operand is falsy and evaluates the right operand.

count[element] is initialized to 0 and then 1 is added to it.

count[element] = 0 + 1

Return object containing the frequency of each element.

return count

keys represent the unique elements in the array.

values denote the number of times each element appears.

["a", "b", "a", "c", "b", "a"] → array

{a: 3, b: 2, c:1} → object

"a" found 3 times

"b" found 2 times

"c" found 1 time


Call the function with ↴

frequency(array1);


Count frequency of each element in array.

const array1 = ["A", "C", "B", "A", "D", "A", "B", "C", "B", "A"];

function frequency(arr) {

const count = {}

for (const element of arr) {

count[element] = (count[element] || 0) + 1;

};

return count;

}

call function

frequency(array1); returns ↴

{A: 4, C: 2, B: 3, D: 1}


Number elements are treated as string elements

[1, 1, 1, "1", "1"] returns ↴

{1: 5}


Alternative using a ternary operator ↴

const array2 = ["A", "C", "B", "A", "D", "A", "B", "C", "B", "A"];

function frequency2(arr) {

const count = {};

for (const element of arr) {

count[element] ? count[element]++ : count[element] = 1;

}

return count;

}

call function

frequency2(array2); returns ↴

{A: 4, C: 2, B: 3, D: 1}

Count frequency of each element