My Solutions and answers to exercise questions in "Introduction to Algorithms" 3rd Edition
For Datastructures/Algorithms
- Short summary of what is is
- Why you use it
- Pros
- Cons
- Example (Code snippet)
Description: A list
- Allows Random Access
- Can be sorted
- Mutable
- Easily convert to string
- Insertion and Deletion are costly because the array must reindexed
var removeElement = function(nums, val) {
//Splice out value
for (let i = nums.length; i >= 0; i--) {
if (nums[i] === val) {
nums.splice(i, 1);
}
}
return nums.length;
};
Description: Object used to store the frequency of items
- Constant time loop up if you have the key vs array.includes
- Can use array methods on Object.keys()
- Store, acces, and delete data
- Caches
- Hashed collisions - Two separate values could create the same hash value.
var intersect = function(nums1, nums2) {
let hash = {};
for (let num of nums1) {
hash[num] = (hash[num] || 0) + 1;
}
let results = [];
for (let num of nums2) {
if (hash[num]) {
hash[num]--;
results.push(num);
}
}
return results;
};