-3
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;

Chetan Jha
  • 19
  • 2
  • If you aren't familiar with that syntax, I'd suggest a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – ChrisMM Nov 11 '21 at 05:43

1 Answers1

5

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;
    } 
  }
}
return string{""};

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, otherwise, return the empty string."