Simple C++ solution using unordered_map


#1

vector Solution::dNums(vector &A, int B) {
unordered_map<int, int> umap;
vector res;
for(int i=0;i<A.size();i++){
if(i<B){
umap[A[i]]++;
}
else{
res.push_back(umap.size());
if(umap[A[i-B]]>1){
umap[A[i-B]]–;
}
else if(umap[A[i-B]]==1){
umap.erase(A[i-B]);
}
umap[A[i]]++;
}
}
res.push_back(umap.size());
return res;
}