In Part 1 we looked at how using two for loops works. In this post, we will look at a practical example to see how this can be implemented.

### Example

So here is the proposed exercise to demonstrate two for loops in action:

**Try to find the most frequent number in an array and how many times it occurs.**

So let’s begin with a function :

1 2 3 4 5 | function mostFrequentNumber(array) { // do something }; |

Next, lets add some variables. highestFrequency will store the amount of highest occurrences. The counter will….well…be our counter. And the item will be the number that occurs the most:

1 2 3 4 5 6 7 | function mostFrequentNumber(array) { var highestFrequency = 0; var counter = 0; var number; }; |

Next, lets add our two for loops:

1 2 3 4 5 6 7 8 9 | function mostFrequentNumber(array) { var highestFrequency = 0; var counter = 0; var number; for (var i = 0; i < array.length; i++) { for (var y = 0; y < array.length; y++) { }; |

Next, we need the second for loop to *do something*. As we look at our first number that our first loop stopped on, we need to compare all the numbers in the array with it to see if we have a match. Thus we will add:

1 2 3 4 5 6 7 8 9 10 11 | function mostFrequentNumber(array) { var highestFrequency = 0; var counter = 0; var number; for (var i = 0; i < array.length; i++) { for (var y = 0; y < array.length; y++) { if (array[i] == array[y]) { counter++; }; |

So if our number in the first loop matches the number in the second loop, the counter increments one. If the number in the first loop matches with the second number of the second loop (second in the array), then the counter increments another, etc, etc. If any do not match, it does not increment the counter and moves on to the next index in the array.

Next, we need to see if the counter is more than the highestFrequency (which is 0). If so, it will assign the amount in the counter to the highestFrequency variable. This will ensure that whenever the counter is at its highest, the highestFrequency will record that number and keep it.

In addition, the number attributed to the highest frequency will also be recorded in the number variable.

1 2 3 4 5 6 7 8 9 10 11 12 13 | function mostFrequentNumber(array) { var highestFrequency = 0; var counter = 0; var number; for (var i = 0; i < array.length; i++) { for (var y = 0; y < array.length; y++) { if (array[i] == array[y]){ counter++; if (highestFrequency < counter) { highestFrequency = counter; number = array[i]; |

Finally, we have to reset the counter after the second loop completes so that we do not add duplicate occurrences. And we can alert our answer.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | function mostFrequentNumber(array) { var highestFrequency = 0; var counter = 0; var number; for (var i = 0; i < array.length; i++) { for (var y = 0; y < array.length; y++) { if (array[i] == array[y]){ counter++; if (highestFrequency < counter) { highestFrequency = counter; number = array[i]; } } } counter=0; } alert("The number " + number + " at " + highestFrequency + " times!"); }; |