Least Frequent Character in String in JavaScript Last Updated : 23 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In JavaScript, finding the least frequent character in a string involves identifying the character that occurs the fewest times within the given string by iterating through the string, counting the occurrences of each character, and determining the one with the minimum frequency. This algorithmic task is essential for analyzing and manipulating strings in JavaScript, contributing to various applications such as data processing, pattern recognition, and text analytics. There are several ways in which this task can be performed using JavaScript which are as follows: Table of Content Using a Frequency Counter ObjectSorting the Character Frequencies ArrayUsing a Frequency Counter ObjectCreate a frequency counter object to count the occurrences of each character in the string to Iterate through the frequency counter object to find the character with the minimum frequency. Example: JavaScript program utilizing an object frequency counter to find the least frequent character in a given string. JavaScript function leastFrequentCharacter(str) { const freqCounter = {}; for (const char of str) { freqCounter[char] = (freqCounter[char] || 0) + 1; } let minFrequency = Infinity; let leastFrequentChar = ''; for (const char in freqCounter) { if (freqCounter[char] < minFrequency) { minFrequency = freqCounter[char]; leastFrequentChar = char; } } return leastFrequentChar; } console.log(leastFrequentCharacter('geeksforgeeks')); Outputf Sorting the Character Frequencies ArrayCreate an array to store the frequencies of each character in the string. Sort the array in ascending order and then return the character corresponding to the minimum frequency in the sorted array. Example: JavaScript program employing object and map structures to find the least frequent character in a string. JavaScript function leastFrequentCharacter(str) { const charFreq = []; for (const char of str) { const index = charFreq.findIndex(item => { return item.char === char }); if (index !== -1) { charFreq[index].count++; } else { charFreq.push({ char, count: 1 }); } } charFreq.sort((a, b) => a.count - b.count); return charFreq[0].char; } console.log(leastFrequentCharacter('geeksforgeeks')); Outputf Comment More infoAdvertise with us Next Article JavaScript- Remove Last Characters from JS String A anjugaeu01 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads JavaScript - Index of a Character in String The indexOf() method can be used to find the first index of specific character in a string. This function returns the index of the given character in a given string. Using indexOf() - Most UsedThe indexOf() method is a method to find the index of the first occurrence of a specific string within a st 3 min read JavaScript- Remove Last Characters from JS String These are the following ways to remove first and last characters from a String:1. Using String slice() MethodThe slice() method returns a part of a string by specifying the start and end indices. To remove the last character, we slice the string from the start (index 0) to the second-to-last charact 2 min read JavaScript - How to Get the Last N Characters of a String? Here are the different methods to get the last N characters of a string in JavaScript.1. Using slice() MethodThe slice() method is the most commonly used approach for extracting the last N characters, thanks to its simplicity and support for negative indices.JavaScriptconst getChar = (s, n) => s. 2 min read Generate Random Characters & Numbers in JavaScript Generate random characters and numbers in JavaScript utilizing Math.random for random numbers and String.fromCharCode() for generating random strings. Combining Math.random and ch.charAt() generates a random mix of characters and numbers within a specified range. Below are the approaches to Generate 2 min read JavaScript - How to Replace Multiple Characters in a String? Here are the various methods to replace multiple characters in a string in JavaScript.1. Using replace() Method with Regular ExpressionThe replace() method with a regular expression is a simple and efficient way to replace multiple characters.JavaScriptconst s1 = "hello world!"; const s2 = s1.replac 3 min read How to Convert Char to String in JavaScript ? In the article, we are going to learn about conversion Char to a string by using JavaScript, Converting a character to a string in JavaScript involves treating the character as a string, a character is a single symbol or letter, while a string is a sequence of characters. Strings can contain multipl 3 min read Like