Easy c++ using hashing O(N)


#1

vector Solution::solve(vector &A) {
unordered_map<int,int>m;
vectorv;
for(int i=0;i<A.size();i++){
if(m.count(A[i])!=0){
int j=m[A[i]];
v[j]++;
if(m.count(v[j])!=0)m[v[j]]=min(m[v[j]],j);
else m[v[j]]=j;
}
m[A[i]]=i;
v.push_back(A[i]);
}

return v;

}


#2

This is giving wrong output for :-
[ 2, 1, 1, 1, 2, 2 ]

The expected output is:- 3 3 2 1 2 2
And, the output your code is giving is:- 3 2 2 1 3 2