Leetcode Note: Go - Kth Distinct String in an Array

Kth Distinct String in an Array - LeetCode
https://leetcode.com/problems/kth-distinct-string-in-an-array/

  • Go 言語で取り組んだメモ

回答

Go solution - Kth Distinct String in an Array - LeetCode
https://leetcode.com/problems/kth-distinct-string-in-an-array/solutions/2458144/go-solution/

func kthDistinct(arr []string, k int) string {
    res, strFrequency := "", make(map[string]int)
    
    for i := 0; i < len(arr); i++ {
        strFrequency[arr[i]]++
    }
    
    for i := 0; i < len(arr); i++ {
        if strFrequency[arr[i]] == 1 {
            k--
        }
        
        if k == 0 {
            res = arr[i]
            break
        }
    }
    
    return res
}