In this tutorial you will learn about the C Program to Find Unique Elements in an Array and its application with practical example.
C Program to Find Unique Elements in an Array
In this tutorial, we will learn to create a C program that will Find Unique Elements in an Array in C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C programming.
- Conditional Statements in C programming.
- Array in C programming.
What is An array?
The array is a collection of similar data types. An array can store multiple values with different indexes in memory by using a single variable. The array can be both single as well as multidimensional.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declare the variables for the program. 2. Take making the array. 3. Adding the elements in the array. 4. Sending that array to the for loop. 5. Print the unique element of array. 6. End the program. |
Program to Find Unique Elements in an Array:-
As we all know array is a collection of similar data type elements. In an array, only one variable is declared which can store multiple values. First will take the number of elements of an array from the user. Then will take the elements from the user for the array. And at last, we will find the unique elements of the array. Printing the unique elements of the array.
With the help of this program, we can Print the unique Array Elements.
Program description:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include <stdio.h> #include <stdlib.h> int uniqueEle(int array[], int n) { int i,j; int flag = 1; for(i = 0; i < n; i++){ for(j = 0; j < n; j++){ if(array[i] == array[j] && i != j) break; } if(j == n ) { //printing the unique elements of the array. printf("\nunique elements in an array is [%d] : %d \n",flag,array[i]); ++flag; } } return -1; } int main() { //declaring the required variables for the program int n,i; //i = it will hold the integer value to control the loop. //n = it will hold the integer value. //Taking the input number of elements in the array form the user printf("\nEnter no: of elements : "); scanf("%d",&n); int array[n]; //Taking the elements of the array form the user printf("\nenter the array elements : "); for(i = 0; i < n; i++){ scanf("%d",&array[i]); } //Finding the unique elements of the array. uniqueEle(array, n); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- n = it will hold the integer value.
- i = it will hold the integer value.
Taking the input number of elements and the elements of the array.
Finding the unique elements in the array.
Printing the unique elements of the array.