We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
First the code goes through each of the elements in magazine, one by one, storing each element in the variable i as it iterates through the array:
for(variofmagazine){map[i]=(map[i]||0)+1;}
Breaking down the part that is probably confusing some people: (map[i] || 0) + 1
What happens here is: if map[i] has not been initialized, it defaults to zero and then one is added. If it has been initialized it simply adds one to its current value. How? Because something || other is a short way of saying if something is defined[*] use it, otherwise use other.
So in short and plain English, what this loop does is it goes through all the words in magazine and keeps count of how many of them there were using the object called map.
After that, the code loops through the ransom note and subtracts one for each element, again initiating at 0 if needed (ransom has been used as name here instead of note in the example):
for(variofransom){map[i]=(map[i]||0)-1;}
Finally, it checks all the values in map to make sure that the ransom loop did not remove words from map to the point where we end up below zero. If it's below zero it means ransom had more occurrences of some word than magazine did.
[*] It's actually a bit more complex than this, what it really looks for is if a value is "truthy," which will be anything that is not one of the following: false, 0, "" (empty string), null, undefined, NaN (Not A Number). You can read more about that here.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Hash Tables: Ransom Note
You are viewing a single comment's thread. Return to all comments →
First the code goes through each of the elements in
magazine
, one by one, storing each element in the variablei
as it iterates through the array:Breaking down the part that is probably confusing some people:
(map[i] || 0) + 1
What happens here is: if
map[i]
has not been initialized, it defaults to zero and then one is added. If it has been initialized it simply adds one to its current value. How? Becausesomething || other
is a short way of sayingif something is defined[*] use it, otherwise use other
.So in short and plain English, what this loop does is it goes through all the words in
magazine
and keeps count of how many of them there were using the object calledmap
.After that, the code loops through the ransom note and subtracts one for each element, again initiating at 0 if needed (
ransom
has been used as name here instead ofnote
in the example):Finally, it checks all the values in map to make sure that the ransom loop did not remove words from
map
to the point where we end up below zero. If it's below zero it meansransom
had more occurrences of some word thanmagazine
did.[*] It's actually a bit more complex than this, what it really looks for is if a value is "truthy," which will be anything that is not one of the following:
false
,0
,"" (empty string)
,null
,undefined
,NaN (Not A Number)
. You can read more about that here.