Algorithm to read duplicate array elements, return first duplicate value only

Hello, I'm sorry if I am asking such a dumb question but this has been annoying me for some time now. I have an algorithm which uses nested for loops, which looks for duplicate elements in an array. However, as soon as one duplicate element is found... the program will stop looking for more duplicates? My program continues to look for more even though one is found? I know there is a break command but I can't get it to work. Code is below:

output of program: Repeating element found first was: 2, 1

Although I want the outcome to be; Repeating element found first was: 2

**Thanks for any help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  #include<stdio.h>
#include<stdlib.h>
#include <iostream>

using namespace std;
void printRepeating(int arr[], int size)
{
  int i, j;
  cout << " Repeating element found first was: " <<endl;
  for(i = 0; i < size; i++)
    for(j = i+1; j < size; j++)
      if(arr[i] == arr[j])
        cout << arr[i] << endl;
        exit;
}

int main()
{
  int arr[] = {4, 2, 1, 5, 2, 3, 1};
  int arr_size = sizeof(arr)/sizeof(arr[0]);
  printRepeating(arr, arr_size);
  getchar();

  return 0;
}
Your code will be interpreted as:

1
2
3
4
5
6
7
8
9
10
11
void printRepeating(int arr[], int size)
{
  int i, j;
  cout << " Repeating element found first was: " <<endl;
  for(i = 0; i < size; i++) {
    for(j = i+1; j < size; j++)
      if(arr[i] == arr[j])
        cout << arr[i] << endl;
  }
   exit;
}


Indenting the code doesn't make it a part of a block in c++. This kind of a thing would work in languages such as python though.
Thanks
zoran404

But my outcome would still be:
Repeating element found first was: 2, 1

Although, I want the outcome to be;

Repeating element found first was: 2
(exit loops and stop looking for more duplicates)

Thanks for the advice, once I learn c++ I was thinking of learning python/java :D

I was telling you what your code does, hoping you'll notice what's wrong...

It should be like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
void printRepeating(int arr[], int size)
{
  int i, j;
  cout << " Repeating element found first was: " <<endl;
  for(i = 0; i < size; i++) {
    for(j = i+1; j < size; j++)
      if(arr[i] == arr[j])
      {
        cout << arr[i] << endl;
        exit;
      }
  }
}
Oh I miss interpreted your first message and iv tried that already
zoran404


My outcome of my program would still display all elements in the array which are duplicates where I only want to show the first duplicate element in the array?

I don't know if I'm pure blind, and im not getting your answer... but I feel stupid lol, thanks
Oh, i didn't notice right away, you have to state exit(0); if you want to quit the program. But notice that the function exit will not return to function main, but will directly quit your program. In case you want to return to main and continue executing your program from there you'll have to use return;
zoran404

You genius, I have been at this for about 1 and a half hours and it's something as simple as that!

Thank-you very much :)
Thanks for the tips too!!
Topic archived. No new replies allowed.