Last active
May 30, 2020 15:39
-
-
Save jherax/51643b2f7622429b9418d9388b4af99f to your computer and use it in GitHub Desktop.
Gets an array of repeated elements between two arrays
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
/** | |
* Gets all repeated values between two arrays | |
* | |
* @param {Array} array1: array to compare | |
* @param {Array} array2: array to compare | |
* @return {Array} | |
*/ | |
function repeatedValues(array1, array2) { | |
return array1.reduce((repeated, value) => { | |
if (~array2.indexOf(value) && !~repeated.indexOf(value)) { | |
repeated.push(value); | |
} | |
return repeated; | |
}, []); | |
} |
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
var a = [1, 5, 8, 5]; | |
var b = [7, 8, 3]; | |
repeatedValues(a, b); | |
// expected: | |
// [8] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment