Last active
October 29, 2015 00:26
-
-
Save greemwahr/667deabcfc4f2c0f0939 to your computer and use it in GitHub Desktop.
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
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