Distinct Numbers in C++
Posted by Samath
Last Updated: January 06, 2021

Write a program that reads in ten numbers and displays distinct numbers (i.e. if a number appears multiple time, it is displayed only once).

Hint: Read a number and store it to an array if it is new. If the number is already in the array, discard it. After the input, the array contains distinct numbers.

#include <iostream.h>
int main(void)
{
      int array[10]={0};
      int new_arr[10000]={0};
      
      
      int key;
 
      int flag = 0;    
 
 
for(int i=0;i<10;i++)
{
	cout<<"Enter a number: ";
    cin>>array[i];		
}
 
for(int j=0;j<10;j++)
{
	key = array[j];
	
      for(int i=0; i<10; i++)    
     {
            if (array[i] == key)  
           {
                   flag = flag+1;     
            }
      }
      
	  if (flag == 1)  
      {
           cout<< array[j]<<endl;
      }
      else if(flag > 1)
      {
      	 new_arr[array[j]] ++;
      	 if(new_arr[array[j]] == 1)
      	 {
      	 	cout<<array[j]<<endl;
         }
      }
     
      flag = 0;   
      }
     return 0;
}