# Return the element that appears for second most number of times in the array JavaScript

In the given problem statement, our task is to return the element that appears for the second most number of times in the array with the help of Javascript. So to solve this problem we will use map function to count the frequency of every element.

## Understanding the problem statement

The given problem is to get the element in the given array which appears for the second most number of times. Suppose we have an array of integer numbers [1, 3, 3, 4, 4, 4] so the function should return 3 as we can see in the array 3 is the second most number of times in the array (2 times).

## Logic for the above problem

For solving this problem we will count the frequency of every element in the array and then we will find the element which has the second most frequency. If there are more elements with the same highest frequency then we have to return the item with the second highest frequency between them. We will use a data structure such as a map to store the frequency count of every element and then we will sort the items of the Map by frequency in descending order to find the item with the second highest frequency.

## Algorithm

Step 1 − Create a function to return the item which appears for the second most number of times in the given array.

Step 2 − Use a freq variable to store the frequency of the second most frequent item in the array.

Step 3 − Use a for loop to traverse the elements of the array.

Step 4 − Use another variable to store the current element of the array. And use freq array to store the frequent element in it.

Step 5 − Now sort the frequent elements to get the required result.

Step 6 − Check that the sorted frequent array length is greater than 1 and equally check that there are similar elements present or not.

Step 7 − If there are multiple elements available with the same highest frequency. Then return the first element.

Step 8 − Otherwise return the most frequent element from the given array.

## Code for the algorithm

//function to return the second most frequent element
function secondMostFrequent(arr) {
const freq = {};
for (let i = 0; i < arr.length; i++) {
const el = arr[i];
freq[el] = (freq[el] || 0) + 1;
}
const sortedFreq = Object.entries(freq).sort((a, b) => b[1] - a[1]);
if (sortedFreq.length > 1 && sortedFreq[0][1] === sortedFreq[1][1]) {
// Multiple elements with the same highest frequency
return Number(sortedFreq[1][0]);
} else {
return Number(sortedFreq[1][0]);
}
}
const arr = [1, 2, 2, 3, 3, 3, 4, 4];
const arr1 = [1, 2, 3, 3, 4, 4, 5, 5, 5];
console.log(secondMostFrequent(arr));
console.log(secondMostFrequent(arr1));


## Complexity

The time taken by the solution is O(n log n) because of the sorting operation. And the space complexity for the code is O(n) as we need to save the frequency counts in the freq object.

## Conclusion

The solution is given an efficient way to get the desired result with the help of Javascript functionalities. Here we convert the keys of the freq items to numbers with the help of Number method before returning them, to ensure that the function actually returns a number not a string.

Updated on: 18-May-2023

78 Views