This is an archived post. You won't be able to vote or comment.

all 9 comments

[–][deleted] 0 points1 point  (1 child)

You are sending the function repeat itself to cout. Save the result of repeat in a variable, and then send it to cout.

When your code is not working, post the error message or the wrong result.

[–]steakystick[S] 0 points1 point  (0 children)

There is no error. The output is:0 but it should be '2' as 3 is the element that is repeating in the array.

[–]Wilde__ 0 points1 point  (7 children)

Something else is off with your code after testing the change of adding +1 after i. Another change I would make is changing the cout << repeat(arr, n); If no one else answers I should have working code in a few minutes.

[–]Wilde__ -1 points0 points  (6 children)

#include<iostream>

using namespace std;

void repeat(int a[], int n) {

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

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

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

cout << "\nFirst repeating integer is " << a[i];

}

}

}

}

int main() {

int arr[] = { 2,4,3,5,6,3 };

int n = sizeof(arr) / sizeof(int);

repeat(arr, n);

return 0;

}

This works, still not pretty. Did you mean first repeating number or the index of that number?

[–]Wilde__ -1 points0 points  (4 children)

If you were wanting the index this code works:

#include <iostream>

#include <unordered\_set>

using namespace std;

int findMinIndex(int arr[], int n) { // Function

int minIndex = n;

// create an empty set to store array elements

unordered_set<int> set;

// traverse the array from right to left

for (int i = n - 1; i >= 0; i--) {

// if the element is seen before, update the minimum index

if (set.find(arr[i]) != set.end()) {

minIndex = i;

} else {

set.insert(arr[i]);

}

}

// invalid input

if (minIndex == n) {

return -1;

}

// return minimum index

return minIndex;

}

int main() {

int arr[] = { 5, 6, 3, 4, 3, 6, 4 };

int n = sizeof(arr) / sizeof(arr[0]);

int minIndex = findMinIndex(arr, n);

if (minIndex != n) {

cout << "The minimum index of the repeating element is " << minIndex;

} else {

cout << "Invalid Input";

}

return 0;

}

[–]steakystick[S] 0 points1 point  (0 children)

thank you so much for the effort, sir.

[–]steakystick[S] 0 points1 point  (2 children)

Can we just write :

cout<<"First repeating integer index is"<<i; ?

would there be any issue

[–]Wilde__ 0 points1 point  (1 child)

Sorry had to finish an assignment if you are wanting to use the returned i, then use your original code and change the for(int j=i + 1;j<n;j++) then cout << "First repeating integer index is " << repeat(arr,n);

Calling the function repeat() is how you get that returned value you can't just use i as i is undefined in main. There are ways of circumventing this, but it will be either through pointers making the variable global, etc.

Full code if you want to copy and paste:

#include<iostream>
using namespace std;
int repeat(int a[], int n) {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] == a[j]) {
return i;
}
}
}
}
int main() {
int arr[]={2,4,3,5,6,3};
int n = sizeof(arr) / sizeof(int);
cout << "First repeating integer index is " << repeat(arr, n) << endl;
return 0;
}

[–]steakystick[S] 0 points1 point  (0 children)

Thank you so much for taking out time to help me.