Logic error?

Feb 15, 2018 at 8:12am
So for this function, it is supposed to remove any duplicate elements in an array and replace them with an empty string. It will also output the number of duplicates that appear in an array. The code will compile and run, and will output the number of duplicates, but it wont replace the duplicates with empty strings. Any suggestions?

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
  int removeDuplicatedValues (string array[], int n)
{
	
	if (n <= 0)
	{
		return -1;
	}
	else
	{
		int dupes_values = 0;

		for (int i = 0; i < n; i++)
		{
			string potential = array[i];

			for (int k = 0; k < n, k != i; k++)
			{
				if (potential == array[k])
				{
					dupes_values++;
					array[k] == "";
				}
			}
		}

	}
}
Feb 15, 2018 at 8:51am
geo28t wrote:
and will output the number of duplicates

No it won't. Most of the time it doesn't return anything or print it out either.

Please post the minimum compileable code that will reproduce your problem.

On efficiency grounds, your k loop need only start at i+1.
Last edited on Feb 15, 2018 at 9:05am
Feb 15, 2018 at 3:48pm
I'd like to play around with this idea and post what I come up with in a bit.
Feb 15, 2018 at 4:35pm
for (int k = 0; k < n, k != i; k++)
is the same as
for (int k = 0; k != i; k++)

The comma operator evaluates the left had expression, throws the result away, and then evaluates the right hand expression. So the result is the value of the right hand expression.

If you want to skip the case where k==i, you could do:
1
2
3
for (int k = 0; k < n; k++) {
    if (k==i) continue;
    ...


But as lastchance said, your k loop should start at i+1. Beyond efficiency, it will give you the correct answer. Otherwise if items 3 and 5 are the same then you'll find the dupe when i=3 and k==5, and then again when i==5 and k==3.
Feb 15, 2018 at 4:57pm
> I'd like to play around with this idea and post what I come up with in a bit.

Spoiler: http://coliru.stacked-crooked.com/a/072c5ab2ab369f2d
Feb 15, 2018 at 5:52pm
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
using namespace std;

int checkArrayForRepeats(int array[],int size);

int main() {
	

	int array[20] = {};
	int size, i = 0, repeats = 0;
	

	cout << "How many numbers you want to be added? \n";
	while (!(cin >> size))
	{
		cout << "Invalid input! Please enter number only " << endl;
		cin.clear();
		cin.ignore(10000, '\n');
		cout << "How many numbers you want to be added? \n";
	}

	cout << "Enter " << size << " numbers: \n" << endl;

	while (i<size)
	{
		while (!(cin >> array[i]))
		{
			cout << "Invalid input! Please enter number only " << endl;
			cin.clear();
			cin.ignore(10000, '\n');
		}
		i++;
	}

	//now we have our array
	cout << "The numbers are: ";
	for (int i = 0; i < size; i++) {
		cout << array[i] << " ";
	}

	cout << endl << endl;

	repeats = checkArrayForRepeats(array, size);

	cout << repeats << " duplicate(s) removed." << endl;

	cout << "Now the numbers are: ";
	for (int i = 0; i < size; i++) {
		if (array[i] != 0) { cout << array[i] << " "; }
	}

	cin.ignore();
	cin.get();
	return 0;
}

int checkArrayForRepeats(int array[], int size) {
	int countRepeats = 0;

	for (int i = 0; i < size; i++) {
		for (int j = i + 1; j < size; j++) {
			if (array[i] == array[j]) { //compares values
				array[i] = 0; //removes duplicates
				countRepeats++; //counts number of duplicates
			}
		}
	}

	return countRepeats;
}


Sorry to keep everyone waiting but they were serving food in the office and I really like cheese cake. Anyways this is not exactly what you are trying to do but the concept is there. It is a starting point for you to play with and model.
Feb 15, 2018 at 5:56pm
That JLBorges,

How does he know how to do everything in half the code... :)
Feb 15, 2018 at 6:39pm
One obvious thing is that he clearly knows the STL.
Better than wandering about him is to learn from him.
Topic archived. No new replies allowed.