MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / kthDistinct

Method kthDistinct

KthDistinctStringInAnArray.java:3–26  ·  view source on GitHub ↗
(String[] arr, int k)

Source from the content-addressed store, hash-verified

1// hashset
2class 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

Callers

nothing calls this directly

Calls 1

addMethod · 0.45

Tested by

no test coverage detected