How to use Hashmap In Javascript

Store the character frequency in a hashmap. Then iterate through the string an find the appropriate character whose frequency is 1 and return the Kth element.

Algorithm:

  • Create a map to store the count of the characters.
  • Iterate through the string and update their frequencies respectively.
  • Again traverse through the string.
  • If frequency of the character is 1, decrement the value of K.
  • Once the value of K reaches 0, return the current character.

Example: In this code we will find K’th Non-repeating Character in string by using hashmap approach.

Javascript
function KthNonRepeatingChar(s, K) {
    const n = s.length;
    
    // Create a map
    let mp = {};
    let ans = null;

    // Iterate through the string
    for (let i = 0; i < n; i++) {
        let ch = s[i];
        if (mp[ch] == undefined) {
            mp[ch] = 1;
        }
        else {
            mp[ch]++;
        }
    }
    
    // Iterate through the string again 
    // and find the Kth non-repeating character
    for (let i = 0; i < n; i++) {
        let ch = s[i];
        if (mp[ch] == 1) {
            K--;
            if (K == 0) {
                ans = ch;
            }
        }
    }

    return ans;
}


let s = "w3wiki";
const K = 3;

// Function call
let ans = KthNonRepeatingChar(s, K);
console.log(ans);

Output
r

Time Complexity: O(n)

Auxiliary Space: O(1)

JavaScript Program to Find K’th Non-Repeating Character in String

The K’th non-repeating character in a string is found by iterating through the string length and counting how many times each character has appeared. When any character is found that appears only once and it is the K’th unique character encountered, it is returned as the result. This operation helps identify the K’th non-repeating character in the string. In this article, we will find K’th Non-repeating Character in a string using JavaScript.

Examples :

Input: s = 'w3wiki' , K = 2
Output: o
Explanation: In the given string, the 2nd non-repeating character is 'o' because 'f' is the first
character that appears only once and 'o' is the next character following it.

Input: 'javascript', K = 4
Output: c
Explanation: In the given string, the 4th non-repeating character is 'c' as 'j', 'v', and 's' are the characters
that appears only once and 'c' is the next character following it.

Table of Content

  • Method 1: Brute Force approach
  • Method 2: Using Hashmap
  • Method 3: Using Map
  • Method 4: Using Queue

Similar Reads

Method 1: Brute Force approach

As a brute force solution, use nested loops to traverse through the string. Starting from the first element, check for every character whether it repeats or not. Use a counter to keep a count of unique elements. When the count reaches K, return the character....

Method 2: Using Hashmap

Store the character frequency in a hashmap. Then iterate through the string an find the appropriate character whose frequency is 1 and return the Kth element....

Method 3: Using Map

This approach employs a Map data structure to efficiently count the occurrences of each character in the string. By utilizing a map, we can achieve a linear time complexity solution....

Method 4: Using Queue

This approach utilizes a queue to keep track of the order of characters as they appear and their frequency. This method leverages the properties of a queue (FIFO) to efficiently find the K’th non-repeating character....