Home »
Data Structure
Find out the element which occurs once in the array
Here is the program to find out the element which occurs once in the array using C:
C program to find the element which occurs once in the array
#include <stdio.h>
// Function to find unique element
int findUniqueElement(int arr[], int arr_size) {
int unique = 0;
for (int i = 0; i < arr_size; i++) {
unique ^= arr[i];
}
return unique;
}
// The main code
int main() {
// array of the numbers
int arr[] = {20, 30, 50, 40, 50, 30, 40};
// length of the array
int arr_size = sizeof(arr) / sizeof(arr[0]);
// finding unique element
int uniqueElement = findUniqueElement(arr, arr_size);
printf("Element that occurs once in the array is: %d\n", uniqueElement);
return 0;
}
Output
Element that occurs once in the array is: 20