find number of repeated elements in array code example

Example 1: hopw to check how many duplicates in an array c

/**
 * C program to count total number of duplicate elements in an array
 */

#include <stdio.h>

#define MAX_SIZE 100  // Maximum array size

int main()
{
    int arr[MAX_SIZE];
    int i, j, size, count = 0;

    /* Input size of array */
    printf("Enter size of the array : ");
    scanf("%d", &size);

    /* Input elements in array */
    printf("Enter elements in array : ");
    for(i=0; i<size; i++)
    {
        scanf("%d", &arr[i]);
    }

    /*
     * Find all duplicate elements in array
     */
    for(i=0; i<size; i++)
    {
        for(j=i+1; j<size; j++)
        {
            /* If duplicate found then increment count by 1 */
            if(arr[i] == arr[j])
            {
                count++;
                break;
            }
        }
    }

    printf("\nTotal number of duplicate elements found in array = %d", count);

    return 0;
}

Example 2: find duplicate in an array using xor

int DuplicateNumber(int arr[], int size){
    int ans=0;
    for(int i=0;i<size;i++){
        ans= ans ^ arr[i] ; 
    }
    for(int i=0;i<=size-2;i++){
        ans= ans ^ i;
    }
    return ans;
   }