How if(!visited[nbr]) is working

as visited is a map so by our code when it has a value it is true only, else the key doesn’t exist in the map. So how are we checking for false statement? I think that a count() function would be much more appropriate.

@abhinav2609 in my understanding, if in a map, a particular key does not exist, its value is initialised by 0. That is why statements like MAP[key]++ are always guaranteed to work even if MAP did not have a key in it beforehand. So that by logic, 0 translates to false, so all nodes would get an initial value of false, if they are not already present in map with some value.

ok so by putting an if statement we are initialising it by false, then checking the same. I got it, Thanks.

1 Like