Help With Deleting Duplicates In Arrays

So I need help with deleting duplicates in my array. I have to delete one of the duplicates and print out the rest.

ex. 1, 2 , 3, 3, 5
print out: 1, 2, 3, 5
The size of the array has to be 20 and the integers needs to be in between 10-100. My code is very messy and I am very new at this. So any help is greatly appreciated. Here i the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
#include<math.h>
#include <ctime>
#include <cstdio>
#include<iomanip>
#include <cstdlib>

using namespace std;

main()
{
    int x=0,b,i,y=0,numbers1, number[20],index,length,counter=0,wow;
    do
    {
        cout << "Enter 20 Numbers Between 10 and 100: ";
        cin >> numbers1;
        number[y] = numbers1;
        y++;
        x++;
    }while(x<20);
    for (i=0; i<20;i++)
    {
        for (b=0; b<20;b++)
        {
           if (number[i] == number[b])
           {
            wow = counter;
            break;
           }
           else
           {

           }

        }
        counter++;

    }
    number[wow] = NULL;
    for (b=0; b<20; b++)
    {
        cout << number[b] << "\n";
    }
}
I hesitate to offer:

memmove(address at location of duplicate, address at location of duplicate +1(location, not byte), sizeinbytes to move)

CHECK THE MEMMOVE syntax. I may have it backwards, I don't use it much and it must be used carefully and precisely!

if you have it sorted and multiple dupes, you can kill all of them at once where +1 becomes +n.

This removes the duplicates.
if you only want to print without showing them, but keep them, you can change the print routine..

int lastnum = number[0];
for (b=0; b<20; b++)
{
if(b && lastnum != number[b])
cout << number[b] << "\n";
lastnum = number[b];
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <random>
#include <vector>
#include <chrono>
#include <algorithm>
#include <iomanip>

size_t SIZE = 20;
constexpr auto low_bound = 10;
constexpr auto up_bound = 100;

int main()
{
    auto seed = std::chrono::system_clock::now().time_since_epoch().count();//seed
    std::default_random_engine dre(seed);//engine
    std::uniform_int_distribution<int> di(low_bound,up_bound);//distribution

    int data[SIZE];
    std::generate(data, data + SIZE, [&dre, &di]{ return di(dre);});
    //http://en.cppreference.com/w/cpp/algorithm/generate

    std::cout << "Original array: \n";
    for (size_t i = 0; i < SIZE; ++i)std::cout << data[i] << " ";

     for(size_t i = 0; i < SIZE; ++i)
    {
        for(size_t j = i+1; j < SIZE; ++j)
        {
            /* If any duplicate found */
            if(data[i] == data[j])
            {
                for( size_t k = j; k  <SIZE; ++k)
                {
                    data[k] = data[k+1];//shift all array elements from thereon leftwards
                }

                /* Decrement size variable after removing one duplicate element */
                --SIZE;

                /* If shifting of elements occur then don't increment j */
                --j;
            }
        }
    }
     std::cout << "\nUnique array: \n";
    for (size_t i = 0; i < SIZE; ++i)std::cout << data[i] << " ";
}
/* POSSIBLE OUTPUT
Original array:
99 21 18 22 68 23 75 53 71 65 90 50 45 25 80 34 85 75 89 61
Unique array:
99 21 18 22 68 23 75 53 71 65 90 50 45 25 80 34 85 89 61
*/
Thanks so much. I got it to work!
for( size_t k = j; k <SIZE; ++k)
{
data[k] = data[k+1];//shift all array elements from thereon leftwards
}

FYI this is identical to what I said, only doing it by hand instead of using the risky (but faster) memory functions. Again, if you can determine that you have more than 2 of the same value, you can delete them in bulk instead of one by one, which works regardless of how you do the data movement.



OP: and, of course, if you used any of the standard library containers like std::vector etc it would make the library facilities also available for erasing duplicates:
sort and remove: http://stackoverflow.com/questions/1041620/whats-the-most-efficient-way-to-erase-duplicates-and-sort-a-vector
don't sort and remove: http://stackoverflow.com/questions/12200486/how-to-remove-duplicates-from-unsorted-stdvector-while-keeping-the-original-or
Topic archived. No new replies allowed.