Can we use a set to store the visited nodes?

Here a map <T, bool> has been used to keep track of visited nodes. Can an unordered_set<T> be used instead? Or does using a map has some other significance?

map is east to handle and more efficent
to check whethe a node is visited or not you have to just write
if( m[node]){}

but in case of set you have to search to all examplesusing
s.find(node)

@asaurabh_26 but s.find() will also work in O(1) if unordered_set is used

yes for unordered_set it is O(1)

use can use set
but map is in more practice by people

1 Like