Remove duplicate elements: Difference between revisions

Added Bracmat examples
(Added Bracmat examples)
Line 62:
<lang awk>$ awk 'BEGIN{split("a b c d c b a",a);for(i in a)b[a[i]]=1;r="";for(i in b)r=r" "i;print r}'
a b c d</lang>
 
=={{header|Bracmat}}==
Here are three solutions. The first one (A) uses a hash table, the second (B) uses a pattern for spotting the elements that have a copy further on in the list and only adds those elements to the answer that don't have copies further on. The third solution (C) utilises an mechanism that is very typical of Bracmat, namely that sums (and also products) always are transformed to a normalised form upon evaluation. Normalisation means that terms are ordered in a unique way and that terms that are equal, apart from a numerical factor, are replaced by a single term with a numerical factor that is the sum of the numerical factors of each term. The answer is obtained by replacing all numerical factors by <code>1</code> as the last step.
 
The list contains atoms and also a few non-atomic expressions. The hash table needs atomic keys, so we apply the <code>str</code> function when searching and inserting elements.
<lang>2 3 5 7 11 13 17 19 cats 222 (-100.2) "+11" (1.1) "+7" (7.) 7 5 5 3 2 0 (4.4) 2:?LIST
 
(A=
( Hashing
= h elm list
. new$hash:?h
& whl
' ( !arg:%?elm ?arg
& ( (h..find)$str$!elm
| (h..insert)$(str$!elm.!elm)
)
)
& :?list
& (h..forall)
$ (
= .!arg:(?.?arg)&!arg !list:?list
)
& !list
)
& put$("Solution A:" Hashing$!LIST \n,LIN)
);
 
(B=
( backtracking
= answr elm
. :?answr
& !arg
: ?
( %?`elm
?
( !elm ?
| &!answr !elm:?answr
)
& ~
)
| !answr
)
& put$("Solution B:" backtracking$!LIST \n,LIN)
);
 
(C=
( summing
= sum car LIST
. !arg:?LIST
& 0:?sum
& whl
' ( !LIST:%?car ?LIST
& (.!car)+!sum:?sum
)
& whl
' ( !sum:#*(.?el)+?sum
& !el !LIST:?LIST
)
& !LIST
)
& put$("Solution C:" summing$!LIST \n,LIN)
);
 
( !A
& !B
& !C
&
)</lang>
Only solution B produces a list with the same order of elements as in the input.
<lang>Solution A: 19 (4.4) 17 11 13 (1.1) (7.) 222 +11 7 5 3 2 0 cats (-100.2) +7
Solution B: 11 13 17 19 cats 222 (-100.2) +11 (1.1) +7 (7.) 7 5 3 0 (4.4) 2
Solution C: (7.) (4.4) (1.1) (-100.2) cats 222 19 17 13 11 7 5 3 2 0 +7 +11</lang>
 
 
=={{header|Brat}}==
483

edits