Is this code too long for searching duplicates in an array

I wrote this code yesterday but I think i wasted time at writing that program because the code looks pretty long for just searching for duplicates in an array
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
26
27
28
29
30
31
32
33
34
  #include "stdafx.h"
#include <string>
#include <iostream>

using namespace std;

int main()
{
	int myarr[5] = { 1,2,3,3,4 }; //the array
	int size = *(&myarr + 1) - myarr; //size of array
	cout << size << endl; //cout size
	bool arrpos[5]; //bool for comfirming
	int sizebool = *(&arrpos + 1) - arrpos; //getting size of bool that comfirmes if the array has a dupl. or not
	int y = 0;
	for (int x = 1; x <= size; x++) //loop that check for every single integer
	{
		if (myarr[x] == myarr[x - 1]) //checks if array x equals to the last one (x-1)
		{
			arrpos[y] = 1; //if one dupl. was found, set bool true
		}
		else if (myarr[x] != myarr[x - 1]) //if there werent any dupl.
		{
			arrpos[y] = 0; //set bool to zero
		}
		for (int l = 0; l <= sizebool; l++) //loop that checks for every subarray
		{
			if (arrpos[l] == 1)
				cout << "the array has a duplicate in it." << endl;
			break;
			}
	}
	cin.get();
	return 0;
}
Last edited on
Do you know a way to search for arrays that doesnt look so long
Last edited on
Sure.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <algorithm>

int main()
{
     int myarr[5] = { 1,2,3,3,4 }; //the array
     for (int i=0; i<5; ++i)
     {
         int * ptr = std::find( myarr+i+1, myarr+5, myarr[i] );
         if (ptr != myarr+5) std::cout << *ptr << " is a duplicate\n";
     }
}

But I wouldn't be surprised if the veterans here in the forum do knowing yet a shorter code.
Do you need to find the duplicate, or just know if there is a duplicate?
One option:
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
26
27
28
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
  const int SIZE = 5;
  int myarr[SIZE] = { 1,2,3,3,4 }; //the array
  sort(myarr, myarr + SIZE);
  bool has_duplicates = false;

  for (int i = 0; i < SIZE - 1; i++)
  {
    if (myarr[i] == myarr[i+1])
    {
      has_duplicates = true;
      break;
    }
  }
  if (has_duplicates)
    cout << "Has duplicates....";
  else
    cout << "Doesn't have duplicates....";

  cin.get();
  return 0;
}

Another option:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <set>

using namespace std;

int main()
{
  const int SIZE = 5;
  int myarr[SIZE] = { 1,2,3,3,4 }; //the array
  set<int> vals(myarr, myarr + SIZE);
  
  if (vals.size() != SIZE)
    cout << "Has duplicates....";
  else
    cout << "Doesn't have duplicates....";

  cin.get();
  return 0;
}
"searching for duplicates in an array" is not a very good problem statement.
Exactly what output do you want?
"Yes" or "no" if there is at least one duplicate or not?
A count of how many duplicates there are?
A list of the indices of the duplicates?
Do you want the duplicates removed from the array?

You are determining the array size in a strange way.

1
2
3
4
    // Instead of this:
    int size = *(&myarr + 1) - myarr;
    // We usually do this
    int size = sizeof myarr / sizeof *myarr;

Don't set bools to 0 and 1; use false and true.

x should only go up to x < size. And l should only go up to l <= sizebool. (And the l loop should presumably be after the x loop, not inside it.)

y doesn't make any sense. Instead, you should just use x.

Don't do an if/else if when the else if is the opposite condition of the if. Just use an if/else.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    // Instead of this
        if (myarr[x] == myarr[x - 1])
            arrpos[x] = true;
        else if (myarr[x] != myarr[x - 1])
            arrpos[x] = false;

    // do this
        if (myarr[x] == myarr[x - 1])
            arrpos[x] = true;
        else
            arrpos[x] = false;

    // But actually, in this case you can do this
        arrpos[x] = (myarr[x] == myarr[x - 1]);

But it's unlikely that you need arrpos at all, unless that bool array itself is the output you are seeking.
Last edited on
Topic archived. No new replies allowed.