#include <stdio.h>
int find_duplicate(int arr[], int n) {
// Your logic here
int i, j, hit;
for (i=0, hit=0; (i < n) && (hit == 0); i++) {
for (j=i+1; j < n; j++) {
if (arr[i] == arr[j]) {
hit = 1;
return arr[i];
}
}
}
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;
}
Solving Approach
starting from top, look for a match in the elements below,
while not found you move down and look for a match in the lower elements only