Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » C Programming » Programs » Arrays » C program to find the unique element in an array
Suryateja Pericherla Categories: Arrays. No Comments on C program to find the unique element in an array
3
(2)

In this article we will learn to implement a C program to find the unique element in an array. A C program is provided below to print the unique element in the given array.

 

Note: This program works only when the values are repeated two times and when there is only one unique value.

 

Program is as follows:

#include<stdio.h>
int main()
{
	int n;
	printf("Enter array size: ");
	scanf("%d", &n);
	int a[n];
	printf("Enter %d numbers: ", n);
	for(int i=0; i<n; i++)
		scanf("%d", &a[i]);
	int temp = a[0];
	for(int i=1; i<n; i++)
	{
		temp = temp ^ a[i];
	}
	printf("Unique element in the array is: %d", temp);
	return 0;
}

 

Input and output for the above program is as follows:

Enter array size: 5
Enter 5 numbers: 1 2 3 2 1
Unique element in the array is: 3

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

Blogarama - Blog Directory