-5

There is a javascript array

var arr = [0, 1, 2, 2, 3, 3, 5];

I want to choose elements that repeats twice. In this case its 2 and 3. and i want attach them into a variable.

var a = 2, b = 3;

As far as i know there is no built-in function to do that job. How can i do that. Thanks.

Webber Depor
  • 198
  • 4
  • 16
  • 1
    Make an effort to solve the problem (and to research it, I'm quite certain this has been asked and answered). If you can't find a previous question on it and can't make your own code work, post a question showing your attempt and asking a specific question about it. – T.J. Crowder Sep 25 '16 at 17:25
  • Exactly twice, or at least twice? – Oriol Sep 25 '16 at 17:25
  • is the array (always) sorted? – Nina Scholz Sep 25 '16 at 17:27
  • @Oriol for my task, its "at least twice" because i am sure they wont be a triple. – Webber Depor Sep 25 '16 at 17:28
  • 2
    This can be done, but it would be more in line with general practice to put them in another array, not in separate variables. – trincot Sep 25 '16 at 17:28
  • @T.J.Crowder i am looking for easiest way or a built-in funtion. I already looked for other questions – Webber Depor Sep 25 '16 at 17:30
  • @WebberDepor: Well, you may not have found it (there are > 10M questions), but [it is here](http://stackoverflow.com/questions/840781/easiest-way-to-find-duplicate-values-in-a-javascript-array). – T.J. Crowder Sep 25 '16 at 17:35

4 Answers4

1

You can use filter to get the values that occur twice.

var arr  = [0, 1, 2, 2, 3, 3, 5];

var dups = arr.filter ( (v,i,a) => a.indexOf(v) < i );

console.log(dups);

In comments you stated you would only have doubles, but no values that occur more than twice. Note that the above would return a value more than once, if the latter would be the case.

This returns the values in an array, which is how you should work. To put them in separate values can be done as follows:

var [a, b, ...others] = dups;

...but you would have to know how many variables to reserve for that, and it does not make your further program any easier. JavaScript has many nice functions (methods) for arrays, so you should in fact leave them in an array.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • See [arrow functions](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions) – trincot Sep 25 '16 at 17:44
0

There is no built in function to do that indeed.

You will have to loop thought the array and keeping track of the number of occurrences of the elements, while building a response array.

Antoine Claval
  • 4,923
  • 7
  • 40
  • 68
0

You could filter a sorted array.

var arr = [0, 1, 2, 2, 3, 3, 5],
    repeats = arr.filter(function (a, i, aa) {
        return aa[i - 1] === a;
    });

console.log(repeats);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Most simple way to do this is the following:

var dups = [];

var arr = [0, 1, 2, 2, 3, 3, 5];

arr.forEach(function (v, i, a){
  delete arr[i];
  if (arr.indexOf(v) !== -1){
    dups.push(v);
  }
});

console.log(dups);

It's destructive however.

Slava Knyazev
  • 5,377
  • 1
  • 22
  • 43