Make a Distinct Digit Array

PROBLEM :
Given an array A[] of n elements.The task is to make a sorted array which will contain all distinct digits present in A[].

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains an integer N, denoting the length of the array. Then in the next line are N space separated integers of the array.

Output:
For each test case in a new line print the distinct array of digits.

Constraints:
1<=T<=100
1<=n<=200
1<=A[]<=1000

Example:
Input:
2
3
131 11 48
4
111 222 333 446

Output:
1 3 4 8
1 2 3 4 6

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
void Distinct_Digit_Array(int [],int ) ;
int main()
 {
int t,no,i ;
int arr[1000] ;
cin>>t ;
while(t--)
{
   cin>>no ;
   for(i=0;i<no;i++)
       cin>>arr[i] ;
     
   Distinct_Digit_Array(arr,no) ;
}
return 0;
}

void Distinct_Digit_Array(int arr[],int no)
{
    int i,r ;
    int temp[10]={0} ;
   
    for(i=0;i<no;i++)
    {
        while(arr[i])
        {
            r=arr[i]%10 ;
            temp[r]++ ;
            arr[i]=arr[i]/10 ;
        }
    }
   
    for(i=0;i<10;i++)
        if(temp[i]!=0)
            cout<<i<<" " ;
           
    cout<<endl ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )