i was trying to solve this problem on leetcode. its question 169 where i have to return if number from the array if its frequency of occuring in the array is more than half the size of the array.
Input: nums = [3,2,3]
Output: 3
Input: nums = [2,2,1,1,1,2,2]
Output: 2
but i wrote the solution and i am getting memory limit exceeded and i dont know why can youplease help me with this?
“
`class Solution {
public:
int majorityElement(vector& nums) {
unordered_map<long long,int>hash;
for(int i = 0; i<nums.size(); i++)
{
hash[nums[i]]++;
}
for(int i = 0;i<hash.size();i++)
{
if(hash[i]>(nums.size()/2))
return i;
}
return -1;
}
};`
“