algorithm intersection(set set preset and set intersectionand smallerset are sets postan intersection of set and set has been created if set count set count smallerset set else smallerset set end if foreach item in smallerset if set contains(itemand set contains(item intersection add(item end if end foreach return intersection end intersection the run time of our intersection algorithm is (nwhere is the number of items in the smaller of the two sets just like our union algorithm linear runtime can only be attained when operating on set with ( insertion unordered sets in the general sense do not enforce the explicit ordering of their members for example the members of { conform to no ordering scheme because it is not required most libraries provide implementations of unordered sets and so dsa does notwe simply mention it here to disambiguate between an unordered set and ordered set we will only look at insertion for an unordered set and cover briefly why hash table is an efficient data structure to use for its implementation insertion an unordered set can be efficiently implemented using hash table as its backing data structure as mentioned previously we only add an item to set if that item is not already in the setso the backing data structure we use must have quick look up and insertion run time complexity hash map generally provides the following ( for insertion approaching ( for look up the above depends on how good the hashing algorithm of the hash table isbut most hash tables employ incredibly efficient general purpose hashing algorithms and so the run time complexities for the hash table in your library of choice should be very similar in terms of efficiency