Function for counting frequency of space separated elements in JavaScript


In this problem statement, our target is to create a function for counting frequency of space separated elements with the help of Javascript functionalities.

Understanding the Problem

The given problem is stating that we have given a space separated elements and our task is to count the frequency of space separated items. So we will provide some strings with some space separated items and assign a result array to see the counts of the frequency of such items. For example if we have a string with space separated items like “Car, Bike, Car, Bicycle, Bike”, in this example we can see that Car is appearing 2 times, Bike is appearing 2 time and Bicycle is appearing 1 time so the output should look like {Car: 2, Bike: 2, Bicycle: 1}.

Logic for the given Problem

First we will create a function to do the given task. This function will take a string as input and then it will split it into an array of elements with the space character as a separator. Then we will iterate over each item and update the frequency accordingly. After that we will see if the item is already present as a key in the frequency then we will increment its count by 1. Otherwise we will add a new key with an initial count of 1. At the end of the function we will return the frequency object.

Algorithm

Step 1: Starting point of this algorithm is to declare a function to count the frequency of the space separated items. Inside this function we will pass a parameter of string as input.

Step 2: After declaring the function, we will split the given string into an array of elements with the help of the split method. And we will use space characters as the separator. This step will return an array in which every element will be a separate word.

Step 3: Create a blank object called freq to keep track of the frequency counts of every item.

Step 4: Traverse over every item in the items array with the help of a for loop.

Step 5: Inside this loop we will retrieve the current item and assign it to a variable called elem.

Step 6: Check that the element already exists as a key in the freq object . If the condition is satisfied then increment its count by 1. If not satisfied then add a new key-value pair to the frequency object with the item as a key and an initial count of 1.

Step 7: So the frequency object contains the counts of every element. Return the freq object as a result.

Example

//Function to count the frequency of spaces
function countFrequency(str) {
   var elem = str.split(" ");
   var freq = {};
   //Traverse the elements of the sentence
   for (var i = 0; i < elem.length; i++) {
      var element = elem[i];

      if (element in freq) {
         freq[element] += 1;
      } else {
         freq[element] = 1;
      }
   }

   return freq;
}
var sentence = "apple banana orange banana apple";
var result = countFrequency(sentence);
console.log(result);

Output

{ apple: 2, banana: 2, orange: 1 }

Complexity

In the code we have used the split method to split the string into an array. So the time complexity is O(n), in which n is the size of the string. As the loop traverses over every item in the array of items and has a time complexity of O(m) here m is the number of items in the array. In the loop the update operations have the time complexity of O(1). So the total time complexity for the code can be calculated as O(n + m).

Conclusion

In the above code we have efficiently solved the given problem for counting the frequency of space separated items in a given string. We have used a loop to traverse the items and a Javascript object to store and update the frequency counts. The code provides the average time complexity of O(n + m), which is efficient for large input strings as well.

Updated on: 14-Aug-2023

147 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements