# How to get the frequency of smallest array value in JavaScript

Are you looking for a way to find the frequency of the smallest array value in JavaScript? If so, you've come to the right place. In this article, we'll show you how to do just that. We'll also provide some history and code samples to help you get started.

So, what is the frequency of the smallest array value? In short, it's the number of times that the smallest array value appears in a given array. To find it, you'll need to use a JavaScript array's `.length` property and a for loop.

Here's a quick example:

``````var fruits = ["apple", "orange", "apple", "banana"];
var frequency = 0;
for (var i = 0; i < fruits.length; i++) {
frequency++;
if (fruits[i] == "apple")
frequency++;
}

console.log(frequency);
``````

In this example, we've created an array called fruits and assigned it some values. We've also created a variable called frequency and set it to 0.

Then, we use a for loop to go through each element in the fruits array. For each element, we increment the frequency variable by 1. We also check to see if the element is equal to "apple". If it is, we increment the frequency variable by 1 again.

Finally, we console.log the value of the frequency variable. This will give us the frequency of the smallest array value. In our example, the frequency is 3. This means that the smallest array value appears 3 times in the fruits array.

## Using Reduce

Another and much more elegant way s to use the `array.reduce()` method. This method takes a function as its parameter, and the function takes two parameters, the current value and the current index. The function returns the frequency of the current value.

The following code shows how to use the array.reduce() method to find the frequency of the smallest value in an array.

``````var frequencies = [1, 2, 2, 3, 3, 5, 5, 7];

var smallest = frequencies[0];

frequencies.reduce(function(prev, curr) {

if (prev === curr) {

return;

}

return prev + 1;

}, 0);

console.log(smallest);
``````

This code prints "2" to the console, because the frequency of the smallest value in the array is 2.

## Using array.find()

Alternatively, you can also use the `array.find()` method. This method takes a function as its parameter, and the function takes two parameters, the current value and the current index. The function returns true if the current value is the smallest value in the array, and false if it is not.

The following code shows how to use the array.find() method to find the frequency of the smallest value in an array.

``````var frequencies = [1, 2, 2, 3, 3, 5, 5, 7];
var smallest = frequencies[0];
frequencies.find(function(curr) {
return curr === smallest;
});

console.log(smallest);
``````

This code prints "2" to the console, because the frequency of the smallest value in the array is 2.

ðŸ’¡
Want to sponsor matcha.fyi? You can have your brand featured right here. Let's connect and chat about it.