Count the total number of duplicate elements in an array with C.
Input array elements: 1 2 3 4 5 1 2 4 5 6
Total number of duplicate elements = 4
#include <stdio.h>int main(){int arr[100];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;}
Output
Enter size of the array : 10Enter elements in array : 1 2 3 4 5 1 2 4 5 6Total number of duplicate elements found in array = 4
0 Comments