Frequency Counting
Frequency counting is a technique that can be used to solve problems what requires tracking, counting, or looking up values quickly. These type of problems often involve a collection of some sort (i.e., array, hashtable, or string). Often the problem will involve matching, counting, or confirming values in the collection.
Implementation
To implement a frequency count, we typically uses a hashtable, However for specific cases, we may opt to use a set, or an array.
Hashtable: General all purpose use
Array: Values in the collection that are of a small range of integer values.
Set: If only needed to track if something exists.
To populate our count we will have to loop through our input collection. This leads to a O(N) time and potentially O(N) auxiliary space (if all values are unique). However future lookups can be performed in O(1) time as a result.
Lets look at examples to see its implementation.
Example 1: Two Sum
Given an array of integers, and a target value determine if there are two integers that add to the sum.
Input: [4,2,6,5,7,9,10]
, 13
Output: true
Brute Force
This problem looks quite similar to the sorted two sum problem, but the input array is not sorted. If we use a brute force approach we could try every single unique pair in the array. This would solve the problem in O(N^2) time.
Try to solve this problem without using hints first. If you get stuck then use the minimum hints to get yourself unstuck. Goodluck!
Hint 1
Try using a hash table (or a set) to store values we have come across to speed up lookup time.
Hint 2
- As we look through each value in the array, what do we need to check in the hash to know whether there is a sum that matches the target?
- If the hash does not contain a matching value, then what should we add to the hash table?
- If the hash does contains a matching value, what should we do?
- If we finish the loop but have not found a match, what should we return?
Solution
function twoSum(numbers, target){
let hash = {};
let current;
for(let i = 0; i < numbers.length; i++) {
current = numbers[i];
if(hash[current]) { return true; }
hash[target - current] = true;
}
return false;
}
Challenge 1: Sort a Bit Array
Given a bit array, return it sorted in-place (a bit array is simply an array that contains only bits, either 0 or 1).
See if you can solve this in O(N) time and O(1) auxiliary space.
Try to solve this using a frequency count rather than using multiple pointers, or using a comparison sort function.
Input : [0, 1, 1, 0, 1, 1, 1, 0]
Output : [0, 0, 0, 1, 1, 1, 1, 1]
Hint 1:
Since there are only two values we could use a two item array
to keep a count of zeros and ones.
Hint 2:
After creating and populating a frequency count, how do we use the number of zeros and number of ones to populate the original input array
.
3 comments