Posts

Showing posts with the label Hashmap

Count Elements With Maximum Frequency - Hashmap [JS]

Description   Solution: Hashmap Store the counts of elements in a hashmap. Keep track of the running max frequency. If a count exceeds the max frequency, reset the total count. If a count is equal to the max frequency, add to the total count. Time Complexity:  O(n) Space Complexity:  O(n) var maxFrequencyElements = function ( nums ) { let maxFreq = 0 , countMap = {}, ans = 0 ; for ( let num of nums) { countMap[num] = (countMap[num] || 0 ) + 1 ; let count = countMap[num]; if (count > maxFreq) { ans = count; maxFreq = count; } else if (count === maxFreq) { ans += count; } } return ans; };

Smallest Missing Non-negative Integer After Operations - Count Mod Values [JS]

Description   Solution: Count Mod Values Numbers will be grouped by their mod value ( nums[i] % value ). Numbers in the same mod group can be transformed into any number with the same mod value. Use a hashmap to count the occurances of numbers for each mod value. Try to create each number from  0  to  n - 1 . If there are no more occurances of numbers for the current mod value, then it is impossible to have a higher MEX. Time Complexity:  O(n) Space Complexity:  O(n) var findSmallestInteger = function ( nums , value ) { let modCount = new Map ( ) ; for ( let num of nums ) { let mod = ( ( num % value ) + value ) % value ; modCount . set ( mod , ( modCount . get ( mod ) || 0 ) + 1 ) ; } for ( let i = 0 ; i < nums . length ; i ++ ) { let modValue = i % value ; if ( modCount . has ( modValue ) && modCount . get ( modValue ) > 0 ) { modCount . set ( modValue , modCount ....