this is my first post here and I would be very happy if you could help me. The task is - Create an array from 6 input numbers, then put the duplicated numbers in another array and then output the array with the repeated numbers.

Do you have any ideas? I'm still a newbie and need some help. Thanks in advance guys !!

EDIT: I'm not sure if I'm on the right way, that's why I didn't post what I've done yet. But this is it:

```
#include <iostream>
using namespace std;
int main()
{
int a[6];
int b[6];
int i,z;
for (i=0; i<6; i++){
cin>>a[i];
}
for (z=0; z<6; z++){
if (a[0]==a[1]) b[z]=a[0];
if (a[0]==a[2]) b[z]=a[0];
if (a[0]==a[3]) b[z]=a[0];
if (a[0]==a[4]) b[z]=a[0];
if (a[0]==a[5]) b[z]=a[0];
if (a[1]==a[2]) b[z]=a[1];
if (a[1]==a[3]) b[z]=a[1];
if (a[1]==a[4]) b[z]=a[1];
if (a[1]==a[5]) b[z]=a[1];
if (a[2]==a[3]) b[z]=a[2];
if (a[2]==a[4]) b[z]=a[2];
if (a[2]==a[5]) b[z]=a[2];
if (a[3]==a[4]) b[z]=a[3];
if (a[3]==a[5]) b[z]=a[3];
if (a[4]==a[5]) b[z]=a[4];
else b[z]=0; cout << b[z];
}
return 0;
}
```

# Best How To :

To give you a better understanding of how to solve this, i'll try to show you what is going on via an example.

Lets say you have just entered the 6 numbers you request via `cin`

, and your `a[]`

variable now looks like this in memory:

```
a[] = { 5, 2, 6, 2, 1, 6 };
```

The duplicates here are 2 and the 6. (pretty obvious for us humans) :-)

You start to compare the first 2 values in memory: `a[0]==a[1]`

, then the first with the third: `a[0]==a[2]`

and so on. If one of these match, you know the value of `a[0]`

has at least one duplicate in memory. Whenever that happens, you would like to do something that that information. Store it somewhere (like your `b[]`

array) or just output it directly with `cout << a[0]`

.

You are now finished with checking `a[0]`

and can continue with `a[1]`

in the same manor, except you do not have to compare with `a[0]`

because you did that in the previous step. Looking at your code, it seems you already understand that you can skip that.

Lets say you really need to store the duplicates. It would help to keep track of how many duplicates you have found.

Pseudo code:

```
duplicates = 0;
if (a[0] has a duplicate) { b[duplicates] = a[0]; duplicates++; }
if (a[1] has a duplicate) { b[duplicates] = a[1]; duplicates++; }
// etc...
```

"has a duplicate" would be like the code you had earlier, like: `a[0]==a[1] || a[0]==a[2] || a[0]==[3]`

and so on.

In your example you have just 6 values, so it is not much work to write all the compare statements yourself. If you needed to do this with many more numbers, it would take you ages to write it, and is prone to little mistakes like typo's. Using a for loop would work for few and many numbers:

Pseude code:

```
duplicates = 0;
for (z = 0 to 6) {
for (y = z+1 to 6) {
if (a[z]==a[y]) {
b[duplicates] = a[z];
duplicates++;
break; // We know it is a duplicate, continue with the next value
}
}
}
```

But even this is not perfect. If one number occurs more than 2 times in memory, this will store the same duplicate value multiple times.