string kthDistinct(vector<string>& arr, int k) {
unordered_map<string, int> m;
for (auto &s : arr)
++m[s];
//Frome here
for (auto &s : arr)
if (m[s] == 1 && --k == 0)
return s;
return "";
//to here
}
Especially in this part
for (auto &s : arr)
if (m[s] == 1 && –k == 0)
return s;
>Solution :
That block of code is a bit on the dense side. If we rewrite the same logic in a more verbose manner, we get:
for (auto &s : arr) {
if( m[s] == 1 ) {
k -= 1;
if( k == 0 ) {
return s;
}
}
Since m contains the number of time a string appears within arr, we can interpret this as:
"Return the kth string of arr that appears only once within arr."