Skip to content

Instantly share code, notes, and snippets.

@tbanj
Last active April 11, 2019 05:22
Show Gist options
  • Save tbanj/d3e529bb881993c83b816563d10febfc to your computer and use it in GitHub Desktop.
Save tbanj/d3e529bb881993c83b816563d10febfc to your computer and use it in GitHub Desktop.
Function to find the duplicates in an array using a hash table
/*
Write a function to find the duplicates in an array using a hash table. Your function should accept an array as an argument,
and return the total number of duplicates in the array.
Hash table is like a bucket -=
Represented as an empty array in this case e.g
harshCheck = [];
O(1) time is from Big O Notation which Big O Notation is use to talk about about how long an algorithm takes to run
With Big O Notation we express the runtime in terms of — how quickly it grows relative to the input, as the input gets larger.
This function runs in O(1) time (or “constant time”) relative to its input. This means that the input array
could be 1 item or 1,000 items, but this function would still just require one “step.”
(1) Loop through the array
(2) At each element check if it exists in the hash table, which has a
lookup of O(1) time
(3) If the element exists in the hash table then it is a duplicate,
if it doesn't exist, insert it into the hash table, also O(1)
*/
/* using linear time complexity to check members
of arrays that appears more than once
*/
// Hash table is a technique in data structure use to
// uniquely identify a specific object from a group of similar objects
// it doesnt allow duplicates entry is being demonstrated in the block of code below
// In hashing, large keys are converted into small keys by using hash functions.
// The values are then stored in a data structure called hash table.
function hashTable (){
this.dataStore = {};
this.size = 0;
}
hashTable.prototype.enumerate = function(){
for(var element in this.dataStore){
console.log(element + " " + this.dataStore[element])
}
return this.dataStore;
}
hashTable.prototype.remove = function(key){
if(this.dataStore.hasOwnProperty(key)){
this.size -= 1;
delete this.dataStore[key];
}
}
hashTable.prototype.size = function(){
return this.size;
}
hashTable.prototype.get = function(){
return this.size > 0 ? this.dataStore : "No data stored";
}
hashTable.prototype.put = function (key, value){
try{
if(!this.dataStore.hasOwnProperty(key)){
this.dataStore[key] = value;
this.size += 1;
return this.dataStore;
} else{
console.log( `duplicate of ${key} key was found, hashtable cannot contain duplicates`);
}
}
catch{
throw new error ("hashtable cannot contain duplicates");
}
}
hashTable.prototype.clear = function(){
this.dataStore = {};
this.size = 0;
return this.dataStore;
}
hashTable.prototype.isEmpty = function(){
return this.size > 0 ? false : true;
}
hashTable.prototype.contains = function(key){
if(this.dataStore.hasOwnProperty(key)){
return true;
}
else {
return false;
}
}
var hashCheck = new hashTable();
// checking when duplicates is being inputted
hashCheck.put("color", "orange");
hashCheck.put("color", "orange");
console.log(hashCheck.put("vehicle_model", "Mercedez Benz"));
// if dataStore isEmpty return false
console.log(hashCheck.isEmpty())
// to remove a key
console.log(hashCheck.remove("color"))
console.log(hashCheck.size);
// check if it contains a particular key
console.log(hashCheck.contains("lapo"));
// to retrieve data store in hashtable
console.log(hashCheck.get());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment