Deleting duplicates in an array

I saw an older post on here asking how to do relatively the same thing, but their approach was different and I don't believe their issue was resolved.

I am attempting to write a program that accepts characters into a 10 character length array. I want the program to evaluate the first array position and delete any duplicates it finds later in the array by identifying a duplicate and moving all of the values to the right of it to the left by one. The 'size' of the array is then decreased by one.

I believe the logic I used for the delete function is correct but the program only prints an 'a' for the first value and the fourth value in the array.

Any help would be greatly appreciated, here is my code:

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
using namespace std;

int letter_entry_print(int size, char array[10]);
int delete_repeats(int& size, char array[10]);
int final_array_print(int size, char array[10]);

int main()
{
	char array[10];
	int size = 10;
	
	letter_entry_print(size,array);
	delete_repeats(size,array);
	final_array_print(size,array);

	cout<<"\n";
	system("pause");
}

int letter_entry_print(int size, char array[10])
{
	int i;

	for (i=0;i<size;i++)
	{
		cout << "Enter letter #" << i+1 << endl;
		cin >> array[i];
		cout << "\n";
	}

	cout << "\nYour array index is as follows:\n\n";
	
	for (i=0;i<size;i++)
	{
		cout << array[i];
		cout << " ";
	}
	
	cout <<"\n\n";
	return 0;
}

int delete_repeats(int& size, char array[10])
{
	int ans;
	int loc;
	int search;
	int replace;
	char target='a';

	cout << "Enter 1 to delete repeats.\n\n";
	cin >> ans;
	if(ans==1)
	{
		for(loc=0;loc<size;loc++)
		{
			array[loc]=target;
			for(search=1;search<(size-loc);search++)
			{
				if(target=array[loc+search])
				{
					for(replace=0;replace<(size-(loc+search));replace++)
					{
						array[loc+search+replace]=array[loc+search+replace+1];
						array[size-1]=0;
						size=(size-1);
					}
				}
			}
		}
	}else(cout<<"\nWhy didn't you press 1?\n\n");
	return 0;
}

int final_array_print(int size, char array[10])
{
	cout<<"\nYour new index is as follows:\n\n";
	int i;
	for(i=0;i<size;i++)
	{
		cout<<array[i];
		cout<<" ";
	}

	cout<<"\n";
	return 0;
}
On line 61 you used a single = instead of ==.
See your line no 58 you had written
array[loc]=target;
which is wrong. you need to initialize target
so,
target = array[loc];
This will solve your problem.
Topic archived. No new replies allowed.