0

I am trying to implement a logic where I have an array [3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6] . I want to find all repeated elements and I want to store these all repeated elements into a new array. I tried very hard but didn't find a solution.

It would be great if someone write simple code and also explain what code doing .

Thanks

Jonas
  • 71
  • 8
  • 1
    May you share the code you did try? – evolutionxbox Sep 14 '21 at 09:21
  • 1
    I found 3 possible solutions is less then 1 minute, what did you try ? –  Sep 14 '21 at 09:22
  • I am beginner, I don't know what's going on – Jonas Sep 14 '21 at 09:22
  • We can't help debug your code if you don't add it to the question. Welcome to SO. You might find reading the site [help section](https://stackoverflow.com/help) useful when it comes to [asking a good question](https://stackoverflow.com/help/how-to-ask), and this [question checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). Code that you've worked on to solve the problem should include a [mcve], and be included in your question. – Andy Sep 14 '21 at 09:23
  • Oke Sir, let do this togheter –  Sep 14 '21 at 09:23
  • 2
    Does this answer your question? [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates) – Harshit Rastogi Sep 14 '21 at 10:06

3 Answers3

0

First of all you need to understand some basics.

Array Map - Information on MDN

Array map creates a new array and doesn't alter your current array.

//So if we do:
[3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6].map((element, index, array) => { return 0; } );
//We get
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Array.indexOf(element) Information on MDN

Returns the index of the element in an array, -1 not found, 0 based.

//So if we do:
[3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6].map( (element, index, array) => { return array.indexOf(element); } );
// we get 
// [0, 1, 2, 3, 3, 5, 3, 7, 2, 7, 1, 3, 12, 7, 1, 15, 16, 7, 18, 5, 1, 3, 22, 0, 12]

//so if we do use the second parameter of map in combination with indexOf
[3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6].map( (element, index, array) => { return array.indexOf(element) === index; } );
// we get 
// [true, true, true, true, false, true, false, true, false, false, false, false, true, false, false, true, true, false, true, false, false, false, true, false, false]

To wrap things up:

[3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6].map( (element, index, array) => { 
    if ( array.indexOf(element) !== index ) { 
       return element; 
    } else { 
        return undefined; 
    }
});
// will give us
// [undefined, undefined, undefined, undefined, 5, undefined, 5, undefined, 63, 2, 4, 5, undefined, 2, 4, undefined, undefined, 2, undefined, 1, 4, 5, undefined, 3, 6]

Array Filter Information on MDN

// so now we have our array of elements that are duplicated with undefined values in it, now we filter.

[undefined, undefined, undefined, undefined, 5, undefined, 5, undefined, 63, 2, 4, 5, undefined, 2, 4, undefined, undefined, 2, undefined, 1, 4, 5, undefined, 3, 6].filter( (element) => { 
    return element !== undefined; 
});
//result
[5, 5, 63, 2, 4, 5, 2, 4, 2, 1, 4, 5, 3, 6]

This is the most basic explanation i can give, no use of conditional operators, storing in variables etc.. if you are a bit more advanced in programming you can write less shorter code, called 1 liners. But i hope this will give you to inspiration to go on with this. But you have to understand the basic first, that means learn, read, learn more and practice a lot.

Karma Blackshaw
  • 880
  • 8
  • 20
  • You could `.filter((e, i, a) => a.indexOf(e) !== i)` directly on the original array. – sp00m Sep 14 '21 at 09:52
  • Woow, easy, I'm just pointing out that the intermediate `.map` step serves no purpose as your final `.filter` can be called on the original array directly, with your exact same `.indexOf` logic. It's not about one-liners, it's about not iterating twice on the array. – sp00m Sep 14 '21 at 10:01
  • Yes and optimizing your code, the most fun part is chasing those μs. But we all had to start somewhere. i suspect this question will be deleted. i should delete it. But new people have some most basic questions about programming and do not know where to go or how to start. –  Sep 14 '21 at 10:05
  • Hahahaha easy guys – Karma Blackshaw Sep 14 '21 at 10:20
0

If you need only repeative items of array you can use this:

var arry = [3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6];
var rep_arry = [];
   
for (const [key,val] of Object.entries(arry)) {
  for (const [key1,val1] of Object.entries(arry)) {
     if(val == val1 && !rep_arry.includes(val) && key != key1){
         rep_arry.push(val);
     }
  }    
}
console.log(rep_arry);

It returns this

[3, 4, 63, 5, 1, 2, 6]

Above code using two for loop which checks each individual elements with each using two for loops. before push I put 3 conditions, 1 for check equals, 2 for already exists check if item available into new array or not and 3rd one for prevent to check by its own element using keys. Its works large amount of items as well. see the snippet.

<html>

    <script>
        var arry = [3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6,3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6];
        var rep_arry = [];
       
        for (const [key,val] of Object.entries(arry)) {
           
            for (const [key1,val1] of Object.entries(arry)) {
                if(val == val1 && !rep_arry.includes(val) && key != key1){
                    rep_arry.push(val);
                }
            }
            
        }
        console.log(rep_arry);
    </script>
</html>
Ranjan
  • 180
  • 2
  • 8
  • This work on a small array, when you have 5000 elements, you will get in troubels. OP asked to explain what code is doing. –  Sep 14 '21 at 10:12
  • Do you think it will get troubles even more items in array ? , – Ranjan Sep 14 '21 at 10:15
  • @PhilAndelhofs, answer to your question, check this https://codepen.io/ranjandaswani/pen/RwgZEev – Ranjan Sep 14 '21 at 10:18
0

We can solve this problem by keeping the list of unique elements. If we will keep the list of unique elements, then it will make easy to collect duplicate elements.

This is maybe not best solution for efficiency, but it's solves the current problem. I commented the code so it maybe will help you to understand what is going on.

To solve this kind of problems, it's very preferable to learn about Algorithms.

const arr = [3,4,63,5,5,1,5,2,63,2,4,5,6,2,4,56,74,2,671,1,4,5,7,3,6];

let uniqueElmts = [];  // here we gonna store unique elements
let duplicates = [];   // here we gonna store duplicate elements 

for(let i = 0; i < arr.length; i++){
   if(uniqueElmts.includes(arr[i])){  // if 'uniqueElmts' already contains arr[i], then arr[i] is a duplicate 
        duplicates.push(arr[i]);  // push arr[i] element to 'duplicates' array, because it's duplicate
   }
   else {
        uniqueElmts.push(arr[i]);     // if 'uniqueElmts' doesn't contain arr[i] then it's unique element so we push arr[i] to 'uniqueElmts' 
   }
}


console.log(duplicates); // [ 5, 5, 63, 2, 4, 5, 2, 4, 2, 1, 4, 5, 3, 6 ]
Hayk
  • 187
  • 1
  • 10