This is a program in C++ that finds the duplicate elements in an integer array.
The main function first declares an integer array myList
and initializes it with 11 elements. The length of the array is then determined by subtracting the value of begin(myList)
from end(myList)
.
The function getPairs
takes two arguments: an integer array myList
and its length length
. This function loops over the elements of the array and checks if any two elements are equal. If so, it prints a message indicating the values of the duplicate elements and their indices.
In the main function, the getPairs
function is called with the arguments myList
and length
. The function is expected to print the duplicate elements and their indices.
Finally, the program returns 0, indicating that it has executed successfully.
#include <iostream>
using namespace std;
void getPairs(int myList[], int length) {
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
if (myList[i] == myList[j]) {
cout << "Number " << myList[i] << " is included two times, at indices " << i << " and " << j << endl;
}
}
}
}
int main() {
int myList[] = { 3, 5, 4, 6, 7, 2, 5, 7, 8, 3, 8 };
int length = end(myList) - begin(myList);
getPairs(myList, length);
return 0;
}
Number 3 is included two times, at indices 0 and 9
Number 5 is included two times, at indices 1 and 6
Number 7 is included two times, at indices 4 and 7
Number 8 is included two times, at indices 8 and 10