Delete repeats from array

I'm working through Walt Savitch's Absolute C++ and an doing Programming Project 2 from Chapter 5 - "Arrays".

The task is detailed below, but basically it is to write a function that removes duplicate characters from an array, then move the preceeding charcacters "down" the array, and reduce it's size.

I decided the best was would be to sort the array alphabetically first, so that any duplicate chars would be next to each other. I've written this code, and it works fine, but I'm really struggling to come up with an algorithm to then remove the duplicates.

I initially had a For loop that runs <x> times, where <x> is the size of the array, and in each loop an If statement that checks if myArr[a] == myArr[a + 1] and if so, make myArr[a + 1] = myArr[a + 2], but this didn't seem to work beyond the first iteration.

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
Write a function called deleteRepeats that has a partially filled array of characters
as a formal parameter and that deletes all repeated letters from the array.
Since a partially filled array requires two arguments, the function will actually
have two formal parameters: an array parameter and a formal parameter of type
int that gives the number of array positions used. When a letter is deleted, the
remaining letters are moved forward to fill in the gap. This will create empty
positions at the end of the array so that less of the array is used. Since the formal
parameter is a partially filled array, a second formal parameter of type int will
tell how many array positions are filled. This second formal parameter will be a
call-by-reference parameter and will be changed to show how much of the array
is used after the repeated letters are deleted. For example, consider the following
code:

char a[10];
a[0] = 'a';
a[1] = 'b';
a[2] = 'a';
a[3] = 'c';
int size = 4;
deleteRepeats(a, size);

After this code is executed, the value of a[0] is 'a', the value of a[1] is 'b', the
value of a[2] is 'c', and the value of size is 3. (The value of a[3] is no longer of
any concern, since the partially filled array no longer uses this indexed variable.)
You may assume that the partially filled array contains only lowercase letters.
Embed your function in a suitable test program.
Topic archived. No new replies allowed.