...
Code Block |
---|
// nums is passed in by reference. (i.e., without making a copy) int len = removeDuplicates(nums); // any modification to nums in your function would be known by the caller. // using the length returned by your function, it prints the first len elements. for (int i = 0; i < len; i++) { print(nums[i]); } |
...
Solution in C++
Code Block |
---|
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
map<int,int> mp;
for(int i=0; i<nums.size(); i++) {
if (!mp[nums[i]]) {
mp[nums[i]]=i;
nums[ mp.size()-1] = nums[i];
}
}
return mp.size();
}
}; |