Help With Deleting Duplicates In Arrays

Well I got my code to delete duplicates when they are right next to each other (ex. 10,11,11,12,13,14 | prints out 10,11,12,13,14), but doesnt find the duplicates when they are spread apart (ex. 10,11,12,11,13,14 | prints out 10,11,12,11,13,14). Any help would be great!
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
  #include<iostream>
#include<math.h>
#include <ctime>
#include <cstdio>
#include<iomanip>
#include <cstdlib>

using namespace std;

main()
{
    int x=0,b,y=0,numbers1, number[20];
    do
    {
        cout << "Enter 20 Numbers Between 10 and 100: ";
        cin >> numbers1;
        if (numbers1 > 100 || numbers1 < 10)
        {
            cout << "Error";
        }
        else
        y++;
        number[y] = numbers1;

        x++;
    }while(x<21);

    int lastnum = number[0];
    for (b=0; b<21; b++)
    {
        if(b && lastnum != number[b])
        cout << number[b] << "\n";
        lastnum = number[b];
    }
}
I changed up my code. I need help with a new problem, not regarding my last problem in this code.
the program sent out yesterday also takes care of non-adjacent duplicates, but you probably you'd like to understand why this program doesn't work as well which is fair enough
Last edited on
To be completely honest, I didn't really understand the other responses. This is my first year of coding and I'm trying to learn to do this by myself.
you can do it with the bubble sort algorithm, but its horrible if you ever need to do it for more than a small array.

basically, grab the first guy in the array, and compare it to every other value. Delete them as you find dupes. Move to second element, repeat... N*N effort loops. Don't forget to loop using size and update size as you delete stuff...!!

if you sort it first, its NlgN much much faster for large N. But you lose your ordering.

There are some other tricks you can play, but the first answer, while brute force, is the easy one.


Last edited on
To be completely honest, I didn't really understand the other responses
... that's not what you said yesterday ...
Thanks so much. I got it to work!

http://www.cplusplus.com/forum/beginner/211980/#msg991799
anyways, my point is not to give you a hard-time but just make sure if you have trouble understanding anything you make it clear upfront rather than declare pre-mature satisfaction. there's no shame in that, we're all learning new stuff all the time
I got duplicates to delete now, but instead of keeping one set, it deletes both. Any fixes to help me keep one set?
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
#include<iostream>
#include<math.h>
#include <ctime>
#include <cstdio>
#include<iomanip>
#include <cstdlib>

using namespace std;

main()
{
    int x=0,SIZE=20,b,y=0,z=0,numbers1, number[SIZE];

    do
    {
        cout << "Enter 20 Numbers Between 10 and 100: ";
        cin >> numbers1;
        if (numbers1 > 100 || numbers1 < 10)
        {
            cout << "Error";
            return 0;
        }
        else
        y++;
        number[y] = numbers1;

        x++;
    }while(x<21);

    int lastnum = number[z];
    int v=1;
    for (b=0; b<21; b++)
    {

        if(lastnum == number[b])
        {

        }
        else
        cout << number[b] << "\n";
        lastnum = number[v];
        if (b == 20)
        {
        v++;
        }
    }
}
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
#include <iostream>

int main()
{
    const int SIZE = 20 ; // *** const
    const int MINV = 20 ;
    const int MAXV = 100 ;

    int number[SIZE] {} ; // initialise to all zeroes
    int cnt = 0 ; // count of numbers entered by the user
    int cnt_unique = 0 ; // count of unique numbers entered into the array

    std::cout << "enter " << SIZE << " integers between " << MINV << " and " << MAXV << '\n' ;

    int n ;
    while( cnt < SIZE && std::cout << '#' << cnt+1 << "? " && std::cin >> n )
    {
        if( n < MINV || n > MAXV )
        {
            std::cout << n << " is not between " << MINV << " and " << MAXV
                      << "\nplease enter another another number\n" ;
        }
        else
        {
            ++cnt ;

            // check if n is a duplicate; if not add it to the array
            bool unique = true ;
            for( int i = 0 ; i < cnt_unique ; ++i )
            {
                if( n == number[i] ) { unique = false ; break ; }
            }

            if(unique) number[cnt_unique++] = n ;
        }
    }

    // print out the unique numbers
    std::cout << "the unique numbers are: " ;
    for( int i = 0 ; i < cnt_unique ; ++i ) std::cout << number[i] << ' ' ;
    std::cout << '\n' ;
}
Topic archived. No new replies allowed.