Kth Distinct String in an Array

A distinct string is a string that is present only once in an array.

Given an array of strings arr, and an integer k, return the k^th distinct string present in arr. If there are fewer than k distinct strings, return an empty string "".

Note that the strings are considered in the order in which they appear in the array.

Example 1
Inputarr = ["d","b","c","b","c","a"], k = 2
Output"a"
The only distinct strings in arr are "d" and "a"; "a" is the 2nd distinct string, so it is returned.
Example 2
Inputarr = ["aaa","aa","a"], k = 1
Output"aaa"
All strings in arr are distinct, so the 1st string "aaa" is returned.

Constraints

  • 1 <= k <= arr.length <= 1000
  • 1 <= arr[i].length <= 5
  • arr[i] consists of lowercase English letters.

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate