w3resource

C Exercises: Find the number occurring odd number of times in an array

C Array: Exercise-34 with Solution

Write a program in C to find the number occurring odd number of times in an array.
All numbers occur even number of times except one number which occurs odd number of times.

Pictorial Presentation:

C Exercises: Find the number occurring odd number of times in an array

Sample Solution:

C Code:

#include <stdio.h>
 
int findOddCountElem (int *arr1, int n ) 
{
     int i, ResultXor = 0; 
     for(i = 0; i < n; i++)
	 {
      ResultXor = ResultXor ^ arr1[i];
	  }
     return ResultXor;
}
 
int main()
{
    int i;
 int arr1[] = {8, 3, 8, 5, 4, 3, 4, 3, 5};
 
    int ctr = sizeof(arr1)/sizeof(arr1[0]);
    printf("The given array is :  ");

	for(i = 0; i < ctr; i++)
	{
	printf("%d  ", arr1[i]);
    } 
    printf("\n");
 
 printf("Number of odd number occur(s) : %d times.\n", findOddCountElem(arr1, ctr));
 return 0;
}

Sample Output:

The given array is :  8  3  8  5  4  3  4  3  5  
Number of odd number occur(s) : 3 times. 

Flowchart :

Flowchart: Find the number occurring odd number of times in an array.

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C to find the majority element of an array.
Next: Write a program in C to find the largest sum of contiguous subarray of an array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

C Programming - What is the argument for printf that formats a long?

Put an l (lowercased letter L) directly before the specifier.

unsigned long n;
long m;

printf("%lu %ld", n, m);

Ref : https://bit.ly/3dIwfkP