228k views
1 vote
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1.

1 Answer

2 votes

Answer:

#include <iostream>

using namespace std;

void findDuplicate(int arr[], int size) {

for(int i = 0; i < size; ++i) {

for(int j = i+1; j < size; ++j) {

if(arr[j] == arr[i]) {

cout << arr[j] << endl;

return;

}

}

}

cout << -1 << endl;

}

int main() {

int arr[] = {2, 3, 5, 6, 11, 20, 4, 8, 4, 9};

findDuplicate(arr, 20);

return 0;

}

Step-by-step explanation: