Created
June 8, 2017 16:54
-
-
Save JacksonRMC/77d6c95ec2abdce8f734fb6f17cd860c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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