Confused by Deleting Repeating Characters in an Arrary

I have a partially filled array consisting of the following ten type char variables:

a[0] = 'a';
a[1] = 'd';
a[2] = 'g';
a[3] = 'f';
a[4] = 'a';
a[5] = 'd';
a[6] = 'x';
a[7] = 't';
a[8] = 'a';
a[9] = 'd';

I need to be able to make a function which will delete any of the same characters after the first time it is in the array. For example, the function would not delete 'a' for a[0], but it would delete 'a' for a[4] and a[8]. Once a character is deleted, all characters beneath it need to move up to fill the gap. So, once all the repeats are deleted, a[4] will equal 'x' since the original variables for a[4] and a[5] are both repeats. The output will just print the remaining variables in order, so the result would be:

a d g f x t

How would I go about making this function?
Last edited on
Something like
1
2
3
4
5
6
7
8
9
10
11
%unique(List, Result)
unique([], []). %an empty list does not have repeated elements
unique([First|List], [First|Result]) :- 
   killall(First, List, Deleted),
   unique(Deleted, Result).

%killall( Element, List, Result )
%Result is the List without Element
killall(_, [], []). %an empty list does not have the element
killall( X, [X|List], Result ) :- killall( X, List, Result ). %The element is at the top, 
killall( X, [Y|List], [Y|Result] ) :- Y=/=X, killall( X, List, Result ).

Last edited on
Topic archived. No new replies allowed.