pushing all duplicate values in array to new array

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());

78 thoughts on “pushing all duplicate values in array to new array”

  1. It is the best time to make some plans for the future and it’s time to be
    happy. I have learn this publish and if I could I desire to suggest you few fascinating
    issues or advice. Perhaps you can write next articles relating to this article.

    I wish to read even more things about it!

    Reply
  2. Pretty section of content. I just stumbled upon your blog and in accession capital to assert that I
    get in fact enjoyed account your blog posts. Any way
    I will be subscribing to your augment and even I achievement you access consistently quickly.

    Reply
  3. Great post. I used to be checking continuously this weblog and I’m
    inspired! Very useful information specifically the last part 🙂 I take care of such information much.

    I used to be looking for this certain information for a long time.
    Thanks and good luck.

    Reply
  4. I do believe all the concepts you have offered to your post.
    They are very convincing and can definitely work.
    Nonetheless, the posts are very quick for beginners.
    Could you please lengthen them a bit from subsequent time?
    Thanks for the post.

    Reply
  5. Greetings! This is my first visit to your blog! We are a
    collection of volunteers and starting a new initiative in a community in the same niche.
    Your blog provided us beneficial information to work on. You have done a marvellous job!

    Reply

Leave a Comment