Skip to content

Instantly share code, notes, and snippets.

@owans
Last active April 8, 2019 18:53
Show Gist options
  • Save owans/633532e2917ac7e35b5b0a5f0d126f89 to your computer and use it in GitHub Desktop.
Save owans/633532e2917ac7e35b5b0a5f0d126f89 to your computer and use it in GitHub Desktop.
Find duplicate nos in array using hash tables

//hash tables are used to iterate over an array using a key to compute the array index, instead of using the key as an array index

//for this function i is used as a key to loop through the array

const showDuplicateNo = (array) => { let hashTable = []; let duplicateNo = [];

for (let i = 0; i < array.length; i++) { if (hashTable[array[i].toString()] === undefined) { hashTable[array[i].toString()] = true; }

else { duplicateNo.push(array[i]); }

}

return duplicateNo; }

console.log(showDuplicateNo([2,8,2,8,10,4]))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment