Hi! :)
I want to print all different elements of sorting array.
input: 10 10 5 4
output: 10 5 4
:-/
Thank you!

5
Contributors
5
Replies
7
Views
6 Years
Discussion Span
Last Post by WaltP

Could you post your code and post your question in a detail manner
Anyway here's my suggestion according to your i/o,
just loop through the array and make a conditional if a value is already given

Print out the first number and also store it in an array.
Get the second number and compare it to the array, and if not found in the array, store in array and print out, else don't print out.
Loop around and do again for each additional number.

void print(int* arr,int n)
{
for(int i=0;i<n-1;i++)
{
if(arr!=arr[i-1])
printf("%d ",arr);
}
printf("%d",arr[n-1]);
}
int main()
{
int N;
cin>>N;
int arr[N];
for(int i=0;i<N;i++)
scanf("%d", &arr);
HeapSort(arr,N);
print(arr,N);
return 0;
}
I want to print all different elements.If we have input: 1 3 4 5 4 6 7 4
output must be 1 3 4 5 6 7

If this is an assignment for a class then the method you have is probably the right kind of way to go. However, if you're doing it for a project of your own, then you can do this in a couple of lines using STL algorithms:

``````// Make an array with your example numbers
int array[] = { 1, 3, 4, 5, 4, 6, 7, 4 };
int array_size = 8;

// Sort the array
std::sort( array, array + array_size );

// Print out the unique values
std::cout << "Unique elements:" << std::endl;
std::copy( array, std::unique( array, array + array_size ), std::ostream_iterator< int >( std::cout, " " ) );
std::cout << std::endl;``````

You need to `#include` algorithm and iterator to use this method :)

Input all the numbers
Sort the numbers
Remove the duplicates
Output what's left

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.