# How to get all duplicate values and push their indexes to an array/object with specific length?

Hey, my javascript array looks like this:

```````var a = ['A', 'A', 'C', 'A', 'C', 'A', 'B', 'B']`
``````

I want to get as result arrays with a specific count of elements (the a-arrays indexes) of the duplicate items, just like this:

e.g. `var length=2`

```````var b = {'A' : [0,1], 'A' : [3,5], 'C' : [2,4], 'B' : [5,6]}`
``````

e.g. `var length=4`

``````` var b = {'A' : [0,1,3,5], 'C' : [2,4], 'B' : [5,6]}`
``````

Or even better not as object, but as array, like this solution:

```````var c = [['A' : [0,1]], ['A' : [3,5]], ['C' : [2,4]], ['B' : [5,6]]}`
``````

So what have you tired?

A first logical step would be to build up an object containing the all of the duplicate values in the array and their indices. Once you have that, it shouldn’t be too hard to display these values in whatever way you need.

I did not get it working.

Fair enough, but what have you tried?

Post your code so we can see what’s wrong with it

2 Likes

Your array, reformatted, would look like

``````a [
0: ‘A’
, 1: ‘A’
, 2: ‘C’
, 3: ‘A’
, 4: ‘C’
, 5: ‘A’
, 6: ‘B’
, 7: ‘B’
]
``````

The array members with common values would be
for ‘A’ - `a[0], a[1], a[3], a[5]`
for ‘B’ - `a[6], a[7]`
for ‘C’ - `a[2], a[4]`

Assuming your example result arrays mistakenly have 5,6 instead of 6,7 for ‘B’, I agree with Pullo’s assessment.

To break down the “first step” into smaller steps, perhaps try putting together some code that reduces the array down to unique values and see how you do with that.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.