Use a Map to Help You Navigate thru the Maze of an Interview

  1. Compared with that of an Array, Object’s key has much more information.
  2. Object keys are unique to each other, making them perfect to tracking things.
  3. When you need something to track historic information, use Object.
If you are given a string that reads "Let me walk you through a real problem to better understand this" you must find the alphabit character that appears most frequently.Solution: The idea is to create a map that entails the frequency information of the string, in the form of:
{
"L":1,
"h":4,
...
}
const strOld="Hello World";
const strNew=strOld.replaceAll(' ','');
function createMap(strNew){
const map={};
for(let i=0; i<strNew.length; i++){
let tmp=strNew[i];
if(map[tmp] && map[tmp]>0){
map[tmp]++;
}else{
map[tmp]=1;
}
}
return map;
}
console.log(createMap(strNew))/* {
d: 1,
e: 1,
H: 1,
l: 3,
o: 2,
r: 1,
W: 1
} */
const hGram=createMap(strNew);const keys   = Object.keys(hGram);
const values = Object.values(hGram);
const sortedValues = [...Object.values(hGram)];
sortedValues.sort((a, b)=> b-a);

const maxValue=sortedValues[0];
const result=keys[values.indexOf(maxValue)];
console.log(result);
//l
let result2;
let max=-1;
for (let k in hGram){
let v=hGram[k];
if(v>max){
max=v;
result2=k;
}
}
console.log(result2, max);
//"l", 3
const nums=[2, 7, 11, 15];
const target=9;
const result=[];
for (let v of nums){
let i=nums.indexOf(v);
let desired= target-v;

if( nums.indexOf(desired) !== -1 ){

result.push(i);
result.push(nums.indexOf(desired));

break;
//use break to stop the loop
}
}
console.log(result);
//[0,1]
const result2=[];
const map={};
for (let i=0; i<nums.length; i++){
let v=nums[i];
let desired=target-v;

if(map[desired] !==undefined){
//note. here must be undefined, because it can be 0/zero!!
result2.push(map[desired]);
result2.push(i);
break;
}else{
map[v]=i;
}
}console.log(result2);
//[0,1]

coder, founder and explorer.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Types of On-Chain Projects So Far… What’s Next?

Web-Based Physical Computing

HOW TO USE DARK OVERLAY IN YOUR CSS STYLING

this,Call, Apply and Bind — Part 1

Create 3D CSS Buttons Which Move As You Mouse Over

Deploying React App to Github Pages

The dev.to project with GatsbyJS-8

Explained : ExpressionChangedAfterItHasBeenCheckedError

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
FRANK GO

FRANK GO

coder, founder and explorer.

More from Medium

Oz for Dummies: Week 2, 2022

5 Steps To Looking 10 Years Younger

Semester in Review: 400 Level 1st Semester

Squid Game Changes Netflix’s Viewership Statistics