I want to make a program to find the repeating integers in an array. For that I had 2 methods.
- Use nested array. it will give a time complexity of O(n²)
- Use an auxiliary array to find the frequency of an array.
I have seen a solution, but it is limited to only 1 digit number. It uses a count array.
int *count = (int *)calloc(sizeof(int), (size - 2));
Why is it (size -2
)?
The code is:
#include<stdio.h>
#include<stdlib.h>
void printDuplicate(int arr[], int size){
int *count = (int *)calloc(sizeof(int),(size-2));
int i;
for(i=0;i<size;i++){
if(count[arr[i]] == 1)
printf("%d,",arr[i]);
else
count[arr[i]]++;
}
}
int main(){
int arr[] = {2,5,3,4,2,5,7,8,7};
int size = sizeof(arr)/sizeof(arr[0]);
printDuplicate(arr,size);
return 0;
}