c code to find duplicate and most repeating element

  • c
  • Write a program to store random numbers into an array of n integers, where the array must contains some duplicates.

    Write a program to store numbers into an array of n integers, where the array must contain some duplicates. Do the following: a) Find out the total number of duplicate elements. b) Find out the most repeating element in the array.

    Code

    #include <stdio.h> #include <stdlib.h> #include <time.h> int maximum(int *arr, int n) { int max = INT_MIN; for (int i = 0; i < n; i++) { if (arr[i] > max) { max = arr[i]; } } return max; } int maximumandindex(int *arr, int n) { int max = INT_MIN; int index; for (int i = 0; i < n; i++) { if (arr[i] > max) { max = arr[i]; index = i; } } return index; } void duplicateandrepeating(int *arr, int n) { int max = maximum(arr, n); int *duparr = (int *)calloc(max, sizeof(int)); for (int i = 0; i < n; i++) { duparr[arr[i]]++; } for (int i = 0; i <= max; i++) { if (duparr[i] > 1) { printf("%d and %d\n", i, duparr[i]); } } printf("most repeating element : %d and %d", maximumandindex(duparr, max+1), maximum(duparr, max+1)); } int main() { clock_t t; t = clock(); int n; printf("enter the size of array : "); scanf("%d", &n); int arr[n]; for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } duplicateandrepeating(arr, n); t = clock() - t; double time_taken = ((double)t) / CLOCKS_PER_SEC; printf("\nfun() took %f seconds to execute \n", time_taken); return 0; }