Skip to content

Instantly share code, notes, and snippets.

@jherax
Last active May 30, 2020 15:39
Show Gist options
  • Save jherax/51643b2f7622429b9418d9388b4af99f to your computer and use it in GitHub Desktop.
Save jherax/51643b2f7622429b9418d9388b4af99f to your computer and use it in GitHub Desktop.
Gets an array of repeated elements between two arrays
/**
* 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;
}, []);
}
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