HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Count the occurrences of a value in a JavaScript array or string

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
javascriptvalueoccurrencescountthearraystring

Problem

I've often had a need to count the occurrences of a specific value in an array or a string. While both of these use-cases seem simple, there are a few intricacies that you need to be aware of.
Array.prototype.reduce() provides the most straightforward way to count the occurrences of a value in an array. Simply increment a counter each time the specific value is encountered inside the array and return the final count.
> [!NOTE]
>
> You can count the occurrences of all values in an array, as described in the previous article.

Solution

const countOccurrences = (arr, val) =>
  arr.reduce((a, v) => (v === val ? a + 1 : a), 0);

countOccurrences([1, 1, 2, 1, 2, 3], 1); // 3


> [!NOTE]
>
> You can count the occurrences of all values in an array, as described in the previous article.
Counting the occurrences of a value in a string is a bit more complex than in an array. You can use Array.prototype.indexOf() to look for the value in the string and increment a counter each time it's found. Update the index to the next position after the value is found and continue the search until the value is no longer found, using a while loop. The loop terminates when Array.prototype.indexOf() returns -1.

Code Snippets

const countOccurrences = (arr, val) =>
  arr.reduce((a, v) => (v === val ? a + 1 : a), 0);

countOccurrences([1, 1, 2, 1, 2, 3], 1); // 3
const countSubstrings = (str, searchValue) => {
  let count = 0,
    i = 0;
  while (true) {
    const r = str.indexOf(searchValue, i);
    if (r !== -1) [count, i] = [count + 1, r + 1];
    else return count;
  }
};

countSubstrings('tiktok tok tok tik tok tik', 'tik'); // 3
countSubstrings('tutut tut tut', 'tut'); // 4

Context

From 30-seconds-of-code: count-occurrences

Revisions (0)

No revisions yet.