
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding the second most frequent character in JavaScript
We are required to write a JavaScript function that takes in a string and returns the character which makes second most appearances in the string.
Therefore, let’s write the code for this function −
Example
The code for this will be −
const str = 'Hello world, I have never seen such a beautiful weather in the world'; const secondFrequent = str => { const map = {}; for(let i = 0; i < str.length; i++){ map[str[i]] = (map[str[i]] || 0) + 1; }; const freqArr = Object.keys(map).map(el => [el, map[el]]); freqArr.sort((a, b) => b[1] - a[1]); return freqArr[1][0]; }; console.log(secondFrequent(str));
Output
The output in the console will be −
e
- Related Articles
- Second most frequent character in a string - JavaScript
- Find Second most frequent character in array - JavaScript
- Returning the second most frequent character from a string (including spaces) - JavaScript
- Program to find second most frequent character in C++
- Find the second most frequent element in array JavaScript
- Finding the most frequent word(s) in an array using JavaScript
- Finding n most frequent words from a sentence in JavaScript
- Python program to find Most Frequent Character in a String
- Most Frequent Subtree Sum in C++
- Finding second smallest word in a string - JavaScript
- Finding first non-repeating character JavaScript
- Most Frequent Number in Intervals in C++
- C# program to find the most frequent element
- Finding the length of second last word in a sentence in JavaScript
- Most frequent element in an array in C++

Advertisements