-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolution.js
56 lines (51 loc) · 1.37 KB
/
Solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* @param {string[]} arr
* @param {number} k
* @return {string}
*/
var kthDistinctss = function (arr, k) {
let a = [];
for (let i = 0; i < arr.length; i++) {
let count = 0;
for (let j = 0; j < arr.length; j++) {
if (arr[i] == arr[j]) {
count += 1;
}
if (count == 2) {
break;
}
}
if (count == 1) {
a.push(arr[i]);
}
}
return a[k - 1] != undefined ? a[k - 1] : "";
};
/*
Runtime: 98 ms, faster than 74.42% of JavaScript online submissions for Kth Distinct String in an Array.
Memory Usage: 44 MB, less than 92.25% of JavaScript online submissions for Kth Distinct String in an Array.
*/
// optimized
var kthDistinct = function (a, k) {
let r = [];
for (let i = 0; i < a.length; i++) {
let c = 0;
for (let j = 0; j < a.length; j++) {
if (a[i] == a[j]) {
c += 1;
}
if (c == 2) { // this break will help the time complexity 118ms 51.16% to 98 ms 74.42%
break;
}
}
if (c == 1) {
r.push(a[i]);
}
}
return r[k - 1] != undefined ? r[k - 1] : "";
};
let arr = ["d", "b", "c", "b", "c", "a"];
let k = 2;
arr = ["aaa", "aa", "a"], k = 1
arr = ["a", "b", "a"], k = 3
console.log(kthDistinct(arr, k));