#include <stdio.h>
int find_duplicate(int arr[], int n) {
// Your logic here
//Double loop
//Loop over every element & compare for double occurance
int occurances=0;
for(int i=0;i<n;i++){ //(0-n) Compare number 0-n
occurances = 0; //Reset occurances
for(int j=0;j<n;j++){ //(0-n)
if(arr[j] == i){ //Check if array element matches 0-n
occurances++; //If it matches, increase occurance
}
if(occurances > 1){ //Check if that occurance has appeared before
return i; //Return the number that has reoccured
}
}
}
return -1;
}
int main() {
int n;
scanf("%d", &n);
int arr[100];
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
int result = find_duplicate(arr, n);
printf("%d", result);
return 0;
}
Input
5 0 1 2 3 2
Expected Output
2