Dutch national flag problem in Javascript

Image
Dutch national flag problem and solution in Javascript Problem statement:   The Dutch national flag (DNF) problem is one of the most popular programming problems proposed by Edsger Dijkstra. The flag of the Netherlands consists of three colors: white, red, and blue. The task is to randomly arrange balls of white, red, and blue such that balls of the same color are placed together. Now, let's consider an array with 3 distinct values say 0, 1 and 2. We won't be using any sort method and we need to sort this array in 0(n). Input Array :  let   arr  = [ 0 ,  2 ,  1 ,  0 ,  1 ,  2 ,  0 ,  2 ]; Expected Output: [ 0, 0, 0, 1, 1, 2, 2, 2 ] Solution Approach : When we see expected output, we can clearly see that sorted array is divided into 3 sections having values 0 , 1 and 2. So, let's divide the array in 3 sections: a) from 0th index to left boundary b) from left boundary to right boundary c) from right boundary to last index. Now we...

Create a Character Map from a String in Javascript

Concept and uses of Character Map in Javascript

 
 

Concept: 


Character Map is basically an object in Javascript which we can create using an string and it can make so many string related problems easier for you.

So, in this post we will take a string "javasolution4u" as input and will create an character map using it. The character map will look like this :


{ '4': 1, j: 1, a: 2, v: 1, s: 1, o: 2, l: 1, u: 2, t: 1, i: 1, n: 1 }


Creating a character map like this can solve many problems like:

  • check for anagrams
  • find the most common character
  • find the count of unique characters in the string

and many more...


So, let's move on the code part now.


 Code implementation:


we will create an function createCharMap which takes str as input which is "javasolution4u" in this example. Below is the code snippet : 


let str = 'javasolution4u';

function createCharMap(str){

    let charMap={};
    for(let char of str ){
        if(!charMap[char]){
        charMap[char]=1;
        }
        else
        charMap[char]++;  
    }
    return charMap;
}


console.log(createCharMap(str))



Output of the above code is :


{ '4': 1, j: 1, a: 2, v: 1, s: 1, o: 2, l: 1, u: 2, t: 1, i: 1, n: 1 }



so, we were able to create an character map, which shows the number of times each character was used in the string str. Now, to show some uses of this character map, we will perform some small operations to get the desired values from our character Map.


Use case#1


Let's see how can we print all the keys of character map i.e all the unique characters in the string


//display all the keys
console.log(Object.keys(createCharMap(str)));


Output of the above line is :


[

  '4', 'j', 'a', 'v',

  's', 'o', 'l', 'u',

  't', 'i', 'n'

]



Use case#2


Let's see how can we print the count of all the keys of character map i.e number of  the unique characters in the string


//count the number of unique chars i.e the number of keys
console.log(Object.keys(createCharMap(str)).length);


Output of the above line is :


11

 

Use case#3


Let's see how we can get the array of all the key and value pairs  of character map i.e characters and their number/times of  the occurrence in the string.


//get an array of key value pair
console.log(Object.entries(createCharMap(str)))


Output of the above line is :

[
  [ '4', 1 ], [ 'j', 1 ],
  [ 'a', 2 ], [ 'v', 1 ],
  [ 's', 1 ], [ 'o', 2 ],
  [ 'l', 1 ], [ 'u', 2 ],
  [ 't', 1 ], [ 'i', 1 ],
  [ 'n', 1 ]
]


Use case#4


Let's see how we can get the value of an specific key of character map i.e how many times any specific character was used in string.


//get the count of any specific character
console.log(createCharMap(str)['a']);


Note: For simplicity, i have used the character "a", if we are not aware whether the character is present or not in string, then we will have to check that first.


Output of the above code is :


2



Conclusion:


So, in this post, we have created a character map using string and also used it in various use cases. We have intentionally used simple use cases for the better understanding purpose. There are numerous use cases where using character map will undoubtedly help to solve many complex string related problems.






Comments

Popular posts from this blog

Ice Cream Parlor : Hackerrank Problem and Solution

Disemvowel Trolls || Codewars problem and solution in Javascript || Topic : Strings and RegEx

Descending Order || CodeWars Problem and solution in javascript.