In: Computer Science
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an array. Print all unique elements of an array Enter the number of elements to be stored in the array: 4 Input 4 elements in the arrangement: element [0]: 3 element [1]: 2 element [2]: 2 element [3]: 5 Expected output: The only items found in the array are: 3 5
Please find the C code for the following:
Code:
#include <stdio.h>
int main()
{
int array[100],count=0,n=0;
//Prompt the user to enter the number of elements in the
array
printf("Enter the number of elements to be stored in the array:
");
scanf("%d",&n);
//Accept the n elements as input from the user
printf("\nInput %d elements in the arrangement: \n",n);
for(int i=0;i<n;i++)
{
printf("element[%d]: ",i);
scanf("%d",&array[i]);
}
printf("The only items found in the array are:\n");
//Iterate through the loop elements and
for(int i=0;i<n;i++)
{
count=0;//Counter to count the occurences of the current
element
//Iterate over the rest of all the elements again
for(int j=0;j<n; j++)
{
//Check if both numbers matches and they should not hold same
index
if(array[i]==array[j] && i!=j)
count++;//Increment the count
}
//If count is 0, then it is unique. So print it
if(count==0)
printf("%d ",array[i]);
}
return 0;
}
Please check the
compiled program and its output for your reference:
Output:
(I believe that I made the code simple and understandable. If you still have any query, Feel free to drop me a comment)
Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...