Created
July 14, 2018 01:12
-
-
Save ambergkim/70e95d37d978d1d694e8a6a29fa07fe9 to your computer and use it in GitHub Desktop.
takes in 2 arrays. find what is the same for both.
This file contains hidden or 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
// only list duplicates once | |
// return array of duplicates | |
// helper findInstances(array) | |
// obj | |
// loop through array | |
// if no key in obj, create key | |
// return obj | |
// main findSimilar(arr1, arr2) | |
// dupes = [] | |
// firstObj = helper(arr1); | |
// secondObj = helper(arr2) | |
// loop through first obj keys | |
// if secondObj[key] | |
// push key in dups | |
// return dupes | |
function findInstances(arr) { | |
let obj = {}; | |
for (let i = 0; i < arr.length; i++) { | |
if (!obj[arr[i]]) { | |
obj[arr[i]] = true; | |
} | |
} | |
return obj. | |
} | |
function findDupes(firstArr, secondArr) { | |
let firstObj = findInstances(firstArr); | |
let secondObj = findInstances(secondArr); | |
let dupes = []; | |
for (key in firstObj) { | |
if (secondObj[key]) { | |
dupes.push(key); | |
} | |
} | |
return dupes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment