Write a program that reads an array and print all elements in array just once (i.e. duplicates elements must be printed just once)
Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>
#define SIZE 100
void main()
{
int A[SIZE];
int N;
int i, j;
do {
printf("Enter N: ");
scanf("%d", &N);
} while (N <= 0 || N > SIZE);
printf("Enter A: ");
for (i = 0;i < N;i++)
scanf("%d", &A[i]);
// we will loop on the array
// and check if the current element repeats
// in the remaining array
// if it's the case, we skip it
// if it's not repeating, we will print it
// we check all the elements
for (i = 0;i < N;i++)
{
// for each element, we check the remaining elements
for(j=i+1; j < N; j++)
{
// if 2 elements are equal,
// we get out from the inner loop
if(A[i]==A[j])
break;
}
// if we exit the loop normally
// i.e. the element is unique, we print it
if(j==N)
printf("%d ", A[i]);
}
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Iterative function to convert a decimal integer to binary