avatarMonu Kumar Modi

Summary

The provided web content describes a JavaScript function that counts the occurrences of each element in an array.

Abstract

The web content outlines a JavaScript function named occurence, which is designed to calculate the frequency of each element within an array. The function takes an array as input and utilizes an object to store the count of each element. It iterates through the array, incrementing the count for each element found, and finally outputs the counts to the console. This function is touted as a valuable tool for data analysis, particularly when identifying the most common elements in a dataset. The article also includes a call to action for readers to support the publication by following it and provides a link to another related article on palindromes in JavaScript.

Opinions

  • The author believes that the occurence function is a useful utility in JavaScript programming, especially for developers working with arrays and data analysis.
  • The article suggests that understanding the frequency of elements in an array can provide insights into the data, implying that this knowledge can be beneficial in various programming scenarios.
  • The inclusion of a full code example indicates the author's intention to provide practical, ready-to-use solutions for readers.
  • By encouraging readers to follow the publication "The Fresh Writes," the author expresses a commitment to supporting small publishers and enhancing the growth of their content.
  • The mention of another article on palindromes in JavaScript implies that the author values the sharing of coding knowledge and encourages continuous learning among readers.

Coding: Counting Occurrences of Elements in an Array using JavaScript

This code snippet is a JavaScript function that can be used to determine the number of occurrences of each element in an array. It’s a useful tool to have in your toolbox when working with arrays, as it allows you to quickly identify the most common elements in a set of data.

Here’s how the code works:

First, the function “occurence” is defined, which takes in an array as an argument.

const occurence = (arr) => {
    let arrLength = arr.length

Next, an empty object called “count” is created to store the number of occurrences of each element in the array.

let count = {}

The function then uses a for loop to iterate through the elements of the input array. For each element, the code checks to see if it already exists as a key in the “count” object.

for(let i =0 ; i<arrLength; i++){
    let num = arr[i]
    count[num] = count[num] ? count[num] + 1 : 1
}

If the key does exist, the value associated with that key is incremented by 1. If the key does not exist, it is added to the object with a value of 1.

let num = arr[i]
count[num] = count[num] ? count[num] + 1 : 1

After the for loop has finished executing, the function uses the console.log() method to print out the “count” object, which now contains the number of occurrences of each element in the input array.

console.log(count)
}

You can test the function by providing an array as an argument.

var arr = [2,3,1,2,38,6,4,0,9]
occurence(arr)

For example, if the input array is [2,3,1,2,38,6,4,0,9], the output would be:

{
  2: 2,
  3: 1,
  1: 1,
  38: 1,
  6: 1,
  4: 1,
  0: 1,
  9: 1
}

This shows that the number 2 occurs twice in the array, while all the other numbers occur once.

You can then use the result of this function to draw some insights, for example, you might use it to find the most common element in an array.

Full Code

const occurence = (arr) => {
    let arrLength = arr.length

let count = {}

for(let i =0 ; i<arrLength; i++){
    let num = arr[i]
    count[num] = count[num] ? count[num] + 1 : 1
}
console.log(count)
}

var arr = [2,3,1,2,38,6,4,0,9]

occurence(arr)

It’s a simple yet powerful function that can be used in various scenarios such as finding the most frequent element in an array, analysing the patterns in data, or even as a helper function in a larger program.

Happy learning 😄

Do support our publication by following it

JavaScript
Coding
Data Structures
Interview
Recommended from ReadMedium