I need help making this function after taking an array and another array (duplicate) that has just the numbers that are duplicated in the first array (for example array=[1,2,3,1,2,3,4,5,6,6], duplicate=[1,2,3,6]).
I want it to return an array as follow: finalArray1=[[1,1],[2,2],[3,3],4,5,[6,6]].
let input = [1,2,4,591,392,391,2,5,10,2,1,1,1,20,20];
let sortArray = array => {
return array.sort(function(a, b) {
return a - b;});
}
function findDuplicates(data) {
let duplicate = [];
data.forEach(function(element, index) {
// Find if there is a duplicate or not
if (data.indexOf(element, index + 1) > -1) {
// Find if the element is already in the duplicate array or not
if (duplicate.indexOf(element) === -1) {
duplicate.push(element);
}
}
});
return duplicate;
}
let newArray = (array, duplicate) => {
for( var i = 0; i < 3; i++ ){
for( var j = 0; j < 15; j++ ){
if( duplicate[i] == array[j] ){
let finalArray = new array().push(array[j]);
}
}
return finalArray;
}
}
duplicatealso have6? And : you never call the functionfindDuplicates. What if original has a value that occurs 3 times? Willduplicateshave unique values still? How will this affect the output?