Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Return the element that appears for second most number of times in the array JavaScript
In JavaScript, finding the element that appears for the second most number of times in an array requires counting frequencies and identifying the second highest count. We'll use a frequency map and sorting to solve this problem efficiently.
Understanding the Problem
Given an array of elements, we need to find the element with the second highest frequency. For example, in the array [1, 3, 3, 4, 4, 4], the element 4 appears 3 times (most frequent), and 3 appears 2 times (second most frequent), so we return 3.
Algorithm Steps
Step 1: Create a frequency map to count occurrences of each element
Step 2: Convert the frequency map to an array of [element, count] pairs
Step 3: Sort the pairs by frequency in descending order
Step 4: Return the element with the second highest frequency
Implementation
function secondMostFrequent(arr) {
// Step 1: Create frequency map
const freq = {};
for (let i = 0; i < arr.length; i++) {
const element = arr[i];
freq[element] = (freq[element] || 0) + 1;
}
// Step 2: Convert to array and sort by frequency
const sortedFreq = Object.entries(freq).sort((a, b) => b[1] - a[1]);
// Step 3: Handle edge cases
if (sortedFreq.length < 2) {
return null; // Not enough unique elements
}
// Step 4: Return second most frequent element
return Number(sortedFreq[1][0]);
}
// Test cases
const arr1 = [1, 2, 2, 3, 3, 3, 4, 4];
const arr2 = [1, 2, 3, 3, 4, 4, 5, 5, 5];
const arr3 = [7, 7, 7, 8, 8, 9];
console.log("Array:", arr1);
console.log("Second most frequent:", secondMostFrequent(arr1));
console.log("\nArray:", arr2);
console.log("Second most frequent:", secondMostFrequent(arr2));
console.log("\nArray:", arr3);
console.log("Second most frequent:", secondMostFrequent(arr3));
Array: [ 1, 2, 2, 3, 3, 3, 4, 4 ] Second most frequent: 2 Array: [ 1, 2, 3, 3, 4, 4, 5, 5, 5 ] Second most frequent: 3 Array: [ 7, 7, 7, 8, 8, 9 ] Second most frequent: 8
Step-by-Step Execution
function secondMostFrequentDetailed(arr) {
console.log("Input array:", arr);
// Count frequencies
const freq = {};
for (let element of arr) {
freq[element] = (freq[element] || 0) + 1;
}
console.log("Frequency map:", freq);
// Sort by frequency
const sortedFreq = Object.entries(freq).sort((a, b) => b[1] - a[1]);
console.log("Sorted frequencies:", sortedFreq);
// Return second most frequent
const result = Number(sortedFreq[1][0]);
console.log("Second most frequent element:", result);
return result;
}
secondMostFrequentDetailed([1, 1, 2, 2, 2, 3, 3, 3, 3]);
Input array: [ 1, 1, 2, 2, 2, 3, 3, 3, 3 ]
Frequency map: { '1': 2, '2': 3, '3': 4 }
Sorted frequencies: [ [ '3', 4 ], [ '2', 3 ], [ '1', 2 ] ]
Second most frequent element: 2
Handling Edge Cases
function secondMostFrequentSafe(arr) {
if (!arr || arr.length === 0) {
return null;
}
const freq = {};
for (let element of arr) {
freq[element] = (freq[element] || 0) + 1;
}
const sortedFreq = Object.entries(freq).sort((a, b) => b[1] - a[1]);
// Check if we have at least 2 unique elements
if (sortedFreq.length < 2) {
return null;
}
return Number(sortedFreq[1][0]);
}
// Test edge cases
console.log("Empty array:", secondMostFrequentSafe([]));
console.log("Single element:", secondMostFrequentSafe([5]));
console.log("All same elements:", secondMostFrequentSafe([1, 1, 1]));
Empty array: null Single element: null All same elements: null
Complexity Analysis
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Frequency counting | O(n) | O(k) where k = unique elements |
| Sorting frequencies | O(k log k) | O(k) |
| Overall | O(n + k log k) | O(k) |
Conclusion
This solution efficiently finds the second most frequent element using a frequency map and sorting approach. The time complexity is dominated by the sorting step, making it O(n + k log k) where k is the number of unique elements.
