Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JacksonRMC/77d6c95ec2abdce8f734fb6f17cd860c to your computer and use it in GitHub Desktop.
Save JacksonRMC/77d6c95ec2abdce8f734fb6f17cd860c to your computer and use it in GitHub Desktop.
function LFUCache(limit) {
this.storage = [];
this.limit = limit;
}
LFUCache.prototype.get = function(key) {
//we want to cycle through the list of objects
for ( let i = 0 ; i < this.storage.length ; i ++ ) {
//check if the key matches the key we are lookinf for
if ( this.storage[i][key] ) {
let temp = this.storage[i];
this.storage.splice(i, 1);
this.storage.push(temp);
return temp[key];
}
}
return -1;
//if it does, splice that object
// add it to the end of the array...it has been used
};
LFUCache.prototype.put = function(key, value) {
let newObj = {};
newObj[key] = value;
if ( this.storage.length === this.limit ) {
this.storage.shift();
}
this.storage.push(newObj);
return this.storage;
//create and object and append it on the array
//unshift the first element
//this is now the newest object in the LFU
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment