(String[] arr, int k)
| 1 | // hashset |
| 2 | class Solution { |
| 3 | public String kthDistinct(String[] arr, int k) { |
| 4 | HashSet<String> distinct = new HashSet<>(); |
| 5 | HashSet<String> duplicate = new HashSet<>(); |
| 6 | for(String str : arr){ |
| 7 | if(duplicate.contains(str)){ |
| 8 | continue; |
| 9 | } |
| 10 | if(distinct.contains(str)){ |
| 11 | distinct.remove(str); |
| 12 | duplicate.add(str); |
| 13 | }else{ |
| 14 | distinct.add(str); |
| 15 | } |
| 16 | } |
| 17 | for(String str : arr){ |
| 18 | if(!duplicate.contains(str)){ |
| 19 | k--; |
| 20 | } |
| 21 | if(k==0){ |
| 22 | return str; |
| 23 | } |
| 24 | } |
| 25 | return ""; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | // hashmap |