vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
vector<string>res;
unordered_map<string,int>m;
int min = INT_MAX;
for(int i = 0; i < list1.size(); i++) m[list1[i]] = i;
for(int i = 0; i < list2.size(); i++)
if(m.count(list2[i]) != 0)
if(m[list2[i]] + i < min) min = m[list2[i]] + i, res.clear(), res.push_back(list2[i]);
else if(m[list2[i]] + i == min) res.push_back(list2[i]);
return res;
}
C++ 9 lines hash table easy to understand


@horanweihaoran As you mentioned,
clear()
operation happens only when we found a smaller one, that is Two lists share a common interest.
 The index sum is smaller than previous one.
Combined
1
and2
, althoughclear()
is a linear time operation, we can see that theclear()
operation will only occur a few times, and in average, it only costs a constant time, that's why the runtime is decent.(BTW,
clear()
is linear, but actually we don't have much data inres
(right?), which makesres.clear()
itself an O(1) operation.)