2206. Divide Array Into Equal Pairs

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
bool divideArray(vector<int>& nums) {
unordered_map<int,int> freq;
for(int n:nums) freq[n]++;
for(auto m:freq) {
if(m.second%2==1) return false;
}
return true;


}
};
1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
bool divideArray(vector<int>& nums) {
vector<int> vec(501,0);
for(int n:nums) vec[n]++;
for(int i=1;i<=500;++i){
if(vec[i]%2==1) return false;
}
return true;
}
};

analysis

  • time complexity O(nlogn)
  • space complexity O(n)