- create a hashmap
- store the frequency counts in the map
- iterate over the map and return the value with the highest frequency
π€What Did I Struggle With?
this above solution is acceptable but i wanted to try and come up with a O(1) space solution
- couldnt figure it out
β Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
π» Implementation of Solution
class Solution {public: int findSpecialInteger(vector<int>& arr) { unordered_map<int, int> frequencyMap; // Store frequency counts in the map for (int num : arr) { frequencyMap[num]++; } // Iterate over the map and find the element with frequency > 25% int n = arr.size(); for (const auto& pair : frequencyMap) { if (pair.second > n / 4) { return pair.first; } } // If no such element is found (though problem guarantees one exists) return -1; }};