All submissions

Find Duplicate in Range 0 and n-1

Code

#include <stdio.h>

int find_duplicate(int arr[], int n) {
    // Your logic here
    int i,j,count=0;			  // intialize the variables with zero
    for(i=0;i<n;i++){			  // iterate the i loop n times
        for(j=0;j<n;j++){         // iterate the j lopp n times
            if(i!=j && (arr[i] == arr[j])){    // chechking the i not j and arr[i] equal arr[j]
            count++;               // condition is ok increment the count
            }
        }
         if(count>1)               // checking the count greater than 1
            return arr[i];         // return the arr[i] number
    }
    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

 

 

int find_duplicate(int arr[], int n) {
    // Your logic here
    int i,j,count=0;			  // intialize the variables with zero
    for(i=0;i<n;i++){			  // iterate the i loop n times
        for(j=0;j<n;j++){         // iterate the j lopp n times
            if(i!=j && (arr[i] == arr[j])){    // chechking the i not j and arr[i] equal arr[j]
            count++;               // condition is ok increment the count
            }
        }
         if(count>1)               // checking the count greater than 1
            return arr[i];         // return the arr[i] number
    }
    return -1;
}
Loading...

Input

5 0 1 2 3 2

Expected Output

2