Skip to content

Instantly share code, notes, and snippets.

@greemwahr
Last active October 29, 2015 00:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greemwahr/667deabcfc4f2c0f0939 to your computer and use it in GitHub Desktop.
Save greemwahr/667deabcfc4f2c0f0939 to your computer and use it in GitHub Desktop.
function duplicates(arr) {
var mergedArray = [].concat.apply([], arr);
console.log(mergedArray);
var arrSorted = mergedArray.sort(function (a, b) {
return a - b;
});
console.log(arrSorted);
var arrayOfDuplicatesArray = arrSorted.reduce(function(acc, val) {
var inner;
if (acc.previous !== val) {
inner = [];
} else {
inner = acc.newArray.pop();
}
inner.push(val);
acc.previous = val;
acc.newArray.push(inner);
return acc;
}, {
previous: null,
newArray: []
});
console.log(arrayOfDuplicatesArray.newArray);
var temporaryArray = arrayOfDuplicatesArray.newArray;
//console.log(temporaryArray)
var arrayOfDuplicates = temporaryArray.filter(function (currElem) {
return currElem.length > 1;
}).map(function (currElem) {
return currElem[0];
})
return arrayOfDuplicates;
//console.log(arrayOfDuplicates);
}
duplicates([0, 1, 2, 4, 4, 3, 3, 1, 5, 3, 3, '5', '5', [2, 2]]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment