Kth Distinct String in an Array

A distinct string is one that appears exactly once in an array. Given an array of strings arr and an integer k, return the k-th distinct string in the order the strings appear in arr. If there are fewer than k distinct strings, return an empty string "".

30:00

Kth Distinct String in an Array
easy
Free written solution
Topics
Companies

A distinct string is one that appears exactly once in an array. Given an array of strings arr and an integer k, return the k-th distinct string in the order the strings appear in arr. If there are fewer than k distinct strings, return an empty string "".

Example 1:
Input: {"arr":["d","b","c","b","c","a"],"k":2}
Output: a
Constraints:
  • 1≤k≤arr.length≤10001 \leq k \leq \text{arr.length} \leq 1000

  • 1≤arr[i].length≤51 \leq \text{arr}[i].\text{length} \leq 5

  • arr[i]\text{arr}[i] consists of lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"arr":["d","b","c","b","c","a"],"k":2}

Pass 1 counts every string. Pass 2 walks arr in order and counts down k = 2 on strings seen exactly once.

pass 1: count
pass 2: find k-th once-only

k left

2

of 2

arr

d

0

b

1

c

2

b

3

c

4

a

5

count map (0 keys)

{ }

Variables
VariableValue
k2
DepthFunction Call
Stack empty
0/13