0

I am trying to get result [1,1,1,1,2,2,20,20] out of below array.

Basically, I am trying to push all duplicate values in new array,However not getting the desired result. Request you to help.

const array = [1, 2, 4, 591, 392, 391, 2, 5, 10, 2, 1, 1, 1, 20, 20];

const dupArray = (arr) => {
  let newArray = array.sort();
  let filteredArray = [];
  for (y = 0; y < newArray.length; y++) {
    for (i = y + 1; i < newArray.length; i++) {
      if (newArray[y] === newArray[i]) {
        filteredArray.push(newArray[i]);
      }
    }
  }

  return filteredArray
};

console.log(dupArray());
VLAZ
  • 26,331
  • 9
  • 49
  • 67
Rahul2021
  • 9
  • 3

5 Answers5

0

I think the expected result should be: [1, 1, 1, 1, 2, 2, 2, 20, 20]

Code:

const array = [1, 2, 4, 591, 392, 391, 2, 5, 10, 2, 1, 1, 1, 20, 20]

const dupArray = (arr) => Object
  .entries(arr.reduce((a, c) => (a[c] = ++a[c] || 1, a), {}))
  .filter(e => e[1] > 1)
  .map(e => Array(e[1]).fill(+e[0]))
  .flat(1)

console.log(dupArray(array))
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
0

First I passed in a custom sorting function inside array.sort() for an increasing sequence. Used a flag hasDuplicates to check if it has had duplicates before, if not then append the element twice to the array. Finally to avoid re-processing of elements, I set y to be i-1.

In terms of efficiency, this should be good as the array is linearly traversed only once.

const array = [1, 2, 4, 591, 392, 391, 2, 5, 10, 2, 1, 1, 1, 20, 20];

const dupArray = (arr) => {
  let newArray = array.sort((a, b) => a - b);  
  let filteredArray = [];
  for (y = 0; y < newArray.length; y++) {
    let hasDuplicates = false;                
    for (i = y + 1; i < newArray.length; i++) {
      if (newArray[y] === newArray[i]) {    
        filteredArray.push(newArray[i]); 
        if(!hasDuplicates) {               
          filteredArray.push(newArray[i]);
          hasDuplicates = true;
        }
      } else {
        y = i - 1;
        break;
      }
    }
  }

  return filteredArray
};

console.log(dupArray());
Major_Ash
  • 199
  • 3
  • 16
  • "*then the result would be [1,1,1,2,2,20]*" -> "*I am trying to get result [1,1,1,1,2,2,20,20]*" – VLAZ Feb 18 '21 at 06:15
  • Shouldn't it be [1,1,1,1,2,2,2,20,20] ? // 2 thrice instead of 2 twice – Major_Ash Feb 18 '21 at 06:21
  • I'm just quoting what OP said. It seems correct to have 2 three times, as it's indeed repeated three times. All the other numbers are expected to show up the same number of times they appear in the input. – VLAZ Feb 18 '21 at 06:23
0

I hope this is usefull for you, Just the duplicates array.

const dub = (arr) => {
  let sortarr = arr.slice().sort(); 
  let getvalue = [];
  for (let i = 0; i < sortarr.length - 1; i++) {
    if (sortarr[i + 1] == sortarr[i]) {
      getvalue.push(sortarr[i]);
    }
  }
  return getvalue;
}

let  array = [1, 2, 4, 591, 392, 391, 2, 5, 10, 2, 1, 1, 1, 20, 20];
alert(` ${array} and Dublicate value ${dub(array)}`);
Divyanshi Mishra
  • 110
  • 1
  • 11
0

You can use a simple filter function

const array = [1, 2, 4, 591, 392, 391, 2, 5, 10, 2, 1, 1, 1, 20, 20];

const dupArray = (arr) => arr.filter( x => 
  arr.filter(y => y === x).length > 1
).sort()

console.log(dupArray(array))

The logic is below

  1. From the array filter where for each elements y if you do a filter for y elements then the size of the array should be greater than 1
  2. Sort the array
Owen Kelvin
  • 14,054
  • 10
  • 41
  • 74
0

Here are two solutions for sorted arrays. I believe the desired output should be [1,1,1,1,2,2,2,20,20]

const array = [1, 2, 4, 591, 392, 391, 2, 5, 10, 2, 1, 1, 1, 20, 20];

//First solution
const dupArray = (arr) => {
  arr = arr.sort((a,b) => a-b);
  let filteredArray = [];
  let currentElement = null;
  for(let i = 0; i < arr.length; i ++){
    if(arr[i] === currentElement){
      filteredArray.push(arr[i]);
    }else{
      currentElement = arr[i];
      if(arr[i] === arr[i+1]){
        filteredArray.push(arr[i]);
      }
    }
  }
  return filteredArray;
};

//Second solution
const dupArray2 = (arr) => {
  arr = arr.sort((a,b) => a-b);
  for(let i = 0; i < arr.length; i ++){
    if(arr[i] != arr[i-1] && arr[i] != arr[i+1]){
      arr.splice(i,1);
      i -= 1;
    }
  }
  return arr;
};
console.log(dupArray(array));
console.log(dupArray2(array));
beartrap
  • 51
  • 2