Distinctive numbers using array

How can you write a program to display this

Enter ten numbers: 1 2 3 2 1 6 3 4 5 2 (press enter)
The distinct numbers are: 1 2 3 6 4 5


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
     bool distinct = false;
     int size = 10; //change this to size you want array to be
     int* array = new int[size]
     //use for loop to enter in values 
    std::cout<<"\nEnter "<<size<<" numbers";
    for (int i = 0; i < size; i++)
    {
        std::cin>>array[i];
    }
    std::cout<<"\nThe distinct numbers are: ";
   for (int i = 0; i < size; i++) //loop to check if each number is distinct
   {
       for (int j = 0; j < size; j++)
       {
              if(arr[i] == arr[j]) //compares number with all other numbers
              {
                      if (j != i) //needed so that array value isn't compared against itself
                      {
                           distinct = false;
                      }
              }
        }
        if(distinct == false)
        {
              std::cout<<arr[i]<<" ";
        }
       distinct = true;
    }


            

Last edited on
here is my 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
const int NUMBERS_SIZE = 10;
	int numbers[NUMBERS_SIZE] = {0};
	bool distinctnumbers = false;
	cout << "Enter ten numbers: ";
	for (int i = 0; i < NUMBERS_SIZE; i++)	
	{
		cin >> numbers[i]; 
	}

	cout << "The distinct numbers are: ";
	for (int i = 0; i < NUMBERS_SIZE; i++)
	{
		for (int j = 0; j < NUMBERS_SIZE; j++)
        {
              if(numbers[i] == numbers[j])
              {
                      if (j != i)
					  {
						  distinctnumbers = false;
					  }
              }
		}
		if (distinctnumbers == false)
		{
			cout << numbers[i] << " ";
		}
		distinctnumbers = true;
	}


Only yield

Enter ten numbers: 1 2 3 2 1 6 3 4 5 2
The distinct numbers are: 1 2 3 2 1 3 2
Please, help!!!
Line 23:
1
2
3
4
5
// if (distinctnumbers == false)
if (distinctnumbers == true)
{
    cout << numbers[i] << " ";
}


The very same logic, written slightly differently:
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
#include <iostream>

int main()
{
    const int NUMBERS_SIZE = 10;
    int numbers[NUMBERS_SIZE] = {0};

    std::cout << "Enter " << NUMBERS_SIZE << " numbers: ";
    for( int i = 0; i < NUMBERS_SIZE; ++i ) std::cin >> numbers[i];

    std::cout << "The distinct numbers are: ";
    for (int i = 0; i < NUMBERS_SIZE; ++i )
    {
        bool distinct = true ;

        for (int j = 0; j < NUMBERS_SIZE; ++j )
        {
            if( numbers[i] == numbers[j] && i != j ) // found duplicate
            {
                distinct = false ;
                break ; // there is no need to check with more numbers
            }
        }

        if(distinct) std::cout << numbers[i] << ' ' ;
    }

    std::cout << '\n' ;
}
It only yields

Enter ten numbers: 1 2 3 2 1 6 3 4 5 2
The distinct numbers are: 6 4 5

The problem required

Enter ten numbers: 1 2 3 2 1 6 3 4 5 2
The distinct numbers are: 1 2 3 6 4 5
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
#include <iostream>

int main()
{
    const int NUMBERS_SIZE = 10;
    int numbers[NUMBERS_SIZE] = {0};

    std::cout << "Enter " << NUMBERS_SIZE << " numbers: ";
    for( int i = 0; i < NUMBERS_SIZE; ++i ) std::cin >> numbers[i];

    std::cout << "The distinct numbers are: ";
    for( int i = 0; i < NUMBERS_SIZE; ++i )
    {
        bool first_occurrence = true ;

        for( int j = 0 ; j < i ; ++j ) //  j < i : check with numbers occurring earlier than i
        {
            if( numbers[i] == numbers[j] ) // if this number was seen earlier
            {
                first_occurrence = false ; // it is not the first occurrence of the number
                break ; // there is no need to check with more numbers
            }
        }

        // if this is the first occurrence of the number, print it out
        if(first_occurrence) std::cout << numbers[i] << ' ' ;
    }

    std::cout << '\n' ;
}
Thanks a lot.
If order doesn't matter, there's also a version for lazy people (like me) :-)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <set>

int main()
{
    const int NUMBERS_SIZE = 10;
    // Let the standard library make all the hard stuff...
    std::set<int> numbers;

    std::cout << "Enter " << NUMBERS_SIZE << " numbers: ";
    for( int i = 0; i < NUMBERS_SIZE; ++i ) {
         int tmp;
         std::cin >> tmp;
         numbers.insert(tmp);
    }

    std::cout << "The distinct numbers are: ";
    for(auto& number : numbers)
        std::cout << number << ' ' ;

    std::cout << '\n' ;
    return 0;
}

Topic archived. No new replies allowed.