-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountElementInKWindow
29 lines (26 loc) · 1.02 KB
/
CountElementInKWindow
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
// Count Distinct Element in Every K Size Window
import java.util.*;
public class Solution {
public static ArrayList<Integer> countDistinctElements(ArrayList<Integer> arr, int k) {
// Write your code here
ArrayList<Integer> num = new ArrayList<Integer>();
Map<Integer,Integer>x = new HashMap<>();
for(int j=0;j<k;j++){
/* if(x.containsKey(arr.get(j)))
x.put(arr.get(j),x.get(arr.get(j))+1);
else x.put(arr.get(j),1); */
x.put(arr.get(j), x.getOrDefault(arr.get(j), 0) + 1);
}
num.add(x.size());
for(int j=k;j<arr.size();j++){
if(x.get(arr.get(j-k))==1) x.remove(arr.get(j-k));
else x.put(arr.get(j-k),x.get(arr.get(j-k))-1);
/* if(x.containsKey(arr.get(j)))
x.put(arr.get(j),x.get(arr.get(j))+1);
else x.put(arr.get(j),1);*/
x.put(arr.get(j), x.getOrDefault(arr.get(j), 0) + 1);
num.add(x.size());
}
return num;
}
}