Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,3 @@
---
from: http://rosettacode.org/wiki/Remove_duplicate_elements
note: Sorting Algorithms

View file

@ -0,0 +1,11 @@
{{Sorting Algorithm}}
[[Category:Sorting]]
Given an Array, derive a sequence of elements in which all duplicates are removed.
There are basically three approaches seen here:
* Put the elements into a hash table which does not allow duplicates. The complexity is O(''n'') on average, and O(''n''<sup>2</sup>) worst case. This approach requires a hash function for your type (which is compatible with equality), either built-in to your language, or provided by the user.
* Sort the elements and remove consecutive duplicate elements. The complexity of the best sorting algorithms is O(''n'' log ''n''). This approach requires that your type be "comparable", i.e., have an ordering. Putting the elements into a self-balancing binary search tree is a special case of sorting.
* Go through the list, and for each element, check the rest of the list to see if it appears again, and discard it if it does. The complexity is O(''n''<sup>2</sup>). The up-shot is that this always works on any type (provided that you can test for equality).
<br><br>

View file

@ -0,0 +1,3 @@
V items = [1, 2, 3, a, b, c, 2, 3, 4, b, c, d]
V unique = Array(Set(items))
print(unique)

View file

@ -0,0 +1,48 @@
* Remove duplicate elements - 18/10/2015
REMDUP CSECT
USING REMDUP,R15 set base register
SR R6,R6 i=0
LA R8,1 k=1
LOOPK C R8,N do k=1 to n
BH ELOOPK
LR R1,R8 k
SLA R1,2
L R9,T-4(R1) e=t(k)
LR R7,R8 k
BCTR R7,0 j=k-1
LOOPJ C R7,=F'1' do j=k-1 to 1 by -1
BL ELOOPJ
LR R1,R7 j
SLA R1,2
L R2,T-4(R1) t(j)
CR R9,R2 if e=t(j) then goto iter
BE ITER
BCTR R7,0 j=j-1
B LOOPJ
ELOOPJ LA R6,1(R6) i=i+1
LR R1,R6 i
SLA R1,2
ST R9,T-4(R1) t(i)=e
ITER LA R8,1(R8) k=k+1
B LOOPK
ELOOPK LA R10,PG pgi=@pg
LA R8,1 k=1
LOOP CR R8,R6 do k=1 to i
BH ELOOP
LR R1,R8 k
SLA R1,2
L R2,T-4(R1) t(k)
XDECO R2,PG+80 edit t(k)
MVC 0(3,R10),PG+89 output t(k) on 3 char
LA R10,3(R10) pgi=pgi+3
LA R8,1(R8) k=k+1
B LOOP
ELOOP XPRNT PG,80 print buffer
XR R15,R15 set return code
BR R14 return to caller
T DC F'6',F'6',F'1',F'5',F'6',F'2',F'1',F'7',F'5',F'22'
DC F'4',F'19',F'1',F'1',F'6',F'8',F'9',F'10',F'11',F'12'
N DC A((N-T)/4) number of T items
PG DC CL92' '
YREGS
END REMDUP

View file

@ -0,0 +1,56 @@
org 100h
jmp test
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Given an array of bytes starting at HL with length BC,
;; remove all duplicates in the array. The new end of the array
;; is returned in HL. A page of memory (256 bytes) is required
;; to mark which bytes have been seen.
uniqpage: equ 3 ; Page to use - a compile-time constant.
; This would need to be set to a page that
; the rest of the program doesn't need.
uniq: xra a ; Zero out the page
lxi d,uniqpage * 256
uniqzero: stax d ; Zero out a byte
inr e ; And do the next byte
jnz uniqzero
mov d,h ; Keep a second pointer to the array in DE
mov e,l
uniqpos: ldax d ; Read from high pointer
mov m,a ; Write to low pointer
inx d ; Increment the high pointer
push h ; Keep low pointer around
mvi h,uniqpage
mov l,a ; Have we seen this byte yet?
cmp m
mov m,a ; No matter what, we've certainly seen it now
pop h ; Bring back the low pointer
jz uniqno ; If we already had it, don't increment low ptr
inx h ; IF we didn't, do increment it
uniqno: dcx b ; One fewer byte left
mov a,b ; If there are zero bytes left,
ora c
rz ; Then return to caller
jmp uniqpos ; Otherwise, do the next byte
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Testing code: read a string from the CP/M console, run
;; uniq, then print the output.
test: lxi d,bufdef ; Read a string
mvi c,10
call 5
lxi d,nl ; Output on new line
mvi c,9
call 5
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
lda bufdef+1 ; Length of input string
mov c,a ; Extend to 16-bit (since uniq supports
mvi b,0 ; long arrays)
lxi h,buf ; Location of input string
call uniq ; Only the unique bytes
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
mvi m,'$' ; Mark the (string) end with '$'
lxi d,buf ; Print the string, which now has had
mvi c,9 ; all duplicates removed.
jmp 5
nl: db 13,10,'$'
bufdef: db 127,0
buf:

View file

@ -0,0 +1 @@
(remove-duplicates xs)

View file

@ -0,0 +1,31 @@
# use the associative array in the Associate array/iteration task #
# this example uses strings - for other types, the associative #
# array modes AAELEMENT and AAKEY should be modified as required #
PR read "aArray.a68" PR
# returns the unique elements of list #
PROC remove duplicates = ( []STRING list )[]STRING:
BEGIN
REF AARRAY elements := INIT LOC AARRAY;
INT count := 0;
FOR pos FROM LWB list TO UPB list DO
IF NOT ( elements CONTAINSKEY list[ pos ] ) THEN
# first occurance of this element #
elements // list[ pos ] := "";
count +:= 1
FI
OD;
# construct an array of the unique elements from the #
# associative array - the new list will not necessarily be #
# in the original order #
[ count ]STRING result;
REF AAELEMENT e := FIRST elements;
FOR pos WHILE e ISNT nil element DO
result[ pos ] := key OF e;
e := NEXT elements
OD;
result
END; # remove duplicates #
# test the duplicate removal #
print( ( remove duplicates( ( "A", "B", "D", "A", "C", "F", "F", "A" ) ), newline ) )

View file

@ -0,0 +1,2 @@
1 2 3 1 2 3 4 1
1 2 3 4

View file

@ -0,0 +1,3 @@
w1 2 3 1 2 3 4 1
((w)=w)/w
1 2 3 4

View file

@ -0,0 +1,90 @@
(* Remove duplicate elements.
This implementation is for elements that have an "equals" (or
"equivalence") predicate. It runs O(n*n) in the number of
elements. *)
#include "share/atspre_staload.hats"
(* How the remove_dups template function will be called. *)
extern fn {a : t@ype}
remove_dups
{n : int}
(eq : (a, a) -<cloref> bool,
src : arrayref (a, n),
n : size_t n,
dst : arrayref (a, n),
m : &size_t? >> size_t m)
:<!refwrt> #[m : nat | m <= n]
void
(* An implementation of the remove_dups template function. *)
implement {a}
remove_dups {n} (eq, src, n, dst, m) =
if n = i2sz 0 then
m := i2sz 0
else
let
fun
peruse_src
{i : int | 1 <= i; i <= n}
{j : int | 1 <= j; j <= i}
.<n - i>.
(i : size_t i,
j : size_t j)
:<!refwrt> [m : int | 1 <= m; m <= n]
size_t m =
let
fun
already_seen
{k : int | 0 <= k; k <= j}
.<j - k>.
(x : a,
k : size_t k)
:<!ref> bool =
if k = j then
false
else if eq (x, dst[k]) then
true
else
already_seen (x, succ k)
in
if i = n then
j
else if already_seen (src[i], i2sz 0) then
peruse_src (succ i, j)
else
begin
dst[j] := src[i];
peruse_src (succ i, succ j)
end
end
prval () = lemma_arrayref_param src (* Prove 0 <= n. *)
in
dst[0] := src[0];
m := peruse_src (i2sz 1, i2sz 1)
end
implement (* A demonstration with strings. *)
main0 () =
let
val eq = lam (x : string, y : string) : bool =<cloref> (x = y)
val src =
arrayref_make_list<string>
(10, $list ("a", "c", "b", "e", "a",
"a", "d", "d", "b", "c"))
val dst = arrayref_make_elt<string> (i2sz 10, "?")
var m : size_t
in
remove_dups<string> (eq, src, i2sz 10, dst, m);
let
prval [m : int] EQINT () = eqint_make_guint m
var i : natLte m
in
for (i := 0; i2sz i <> m; i := succ i)
print! (" ", dst[i] : string);
println! ()
end
end

View file

@ -0,0 +1,198 @@
(* Remove duplicate elements.
This implementation is for elements that have an "equals" (or
"equivalence") predicate. It runs O(n*n) in the number of
elements. It uses a linked list and supports linear types.
The equality predicate is implemented as a template function. *)
#include "share/atspre_staload.hats"
staload UN = "prelude/SATS/unsafe.sats"
#define NIL list_vt_nil ()
#define :: list_vt_cons
(*------------------------------------------------------------------*)
(* Interfaces *)
extern fn {a : vt@ype}
array_remove_dups
{n : int}
{p_arr : addr}
(pf_arr : array_v (a, p_arr, n) |
p_arr : ptr p_arr,
n : size_t n)
:<!wrt> [m : nat | m <= n]
@(array_v (a, p_arr, m),
array_v (a?, p_arr + (m * sizeof a), n - m) |
size_t m)
extern fn {a : vt@ype}
list_vt_remove_dups
{n : int}
(lst : list_vt (a, n))
:<!wrt> [m : nat | m <= n]
list_vt (a, m)
extern fn {a : vt@ype}
remove_dups$eq :
(&a, &a) -<> bool
extern fn {a : vt@ype}
remove_dups$clear :
(&a >> a?) -< !wrt > void
(*------------------------------------------------------------------*)
(* Implementation of array_remove_dups *)
(* The implementation for arrays converts to a list_vt, does the
removal duplicates, and then writes the data back into the original
array. *)
implement {a}
array_remove_dups {n} {p_arr} (pf_arr | p_arr, n) =
let
var lst = array_copy_to_list_vt<a> (!p_arr, n)
var m : int
val lst = list_vt_remove_dups<a> lst
val m = list_vt_length lst
prval [m : int] EQINT () = eqint_make_gint m
prval @(pf_uniq, pf_rest) =
array_v_split {a?} {p_arr} {n} {m} pf_arr
val () = array_copy_from_list_vt<a> (!p_arr, lst)
in
@(pf_uniq, pf_rest | i2sz m)
end
(*------------------------------------------------------------------*)
(* Implementation of list_vt_remove_dups *)
(* The list is worked on "in place". That is, no nodes are copied or
moved to new locations, except those that are removed and freed. *)
fn {a : vt@ype}
remove_equal_elements
{n : int}
(x : &a,
lst : &list_vt (a, n) >> list_vt (a, m))
:<!wrt> #[m : nat | m <= n]
void =
let
fun {a : vt@ype}
remove_elements
{n : nat}
.<n>.
(x : &a,
lst : &list_vt (a, n) >> list_vt (a, m))
:<!wrt> #[m : nat | m <= n]
void =
case+ lst of
| NIL => ()
| @ (head :: tail) =>
if remove_dups$eq (head, x) then
let
val new_lst = tail
val () = remove_dups$clear<a> head
val () = free@{a}{0} lst
val () = lst := new_lst
in
remove_elements {n - 1} (x, lst)
end
else
let
val () = remove_elements {n - 1} (x, tail)
prval () = fold@ lst
in
end
prval () = lemma_list_vt_param lst
in
remove_elements {n} (x, lst)
end
fn {a : vt@ype}
remove_dups
{n : int}
(lst : &list_vt (a, n) >> list_vt (a, m))
:<!wrt> #[m : nat | m <= n]
void =
let
fun
rmv_dups {n : nat}
.<n>.
(lst : &list_vt (a, n) >> list_vt (a, m))
:<!wrt> #[m : nat | m <= n]
void =
case+ lst of
| NIL => ()
| head :: NIL => ()
| @ head :: tail =>
let
val () = remove_equal_elements (head, tail)
val () = rmv_dups tail
prval () = fold@ lst
in
end
prval () = lemma_list_vt_param lst
in
rmv_dups {n} lst
end
implement {a}
list_vt_remove_dups {n} lst =
let
var lst = lst
in
remove_dups {n} lst;
lst
end
(*------------------------------------------------------------------*)
implement
remove_dups$eq<Strptr1> (s, t) =
($UN.strptr2string s = $UN.strptr2string t)
implement
remove_dups$clear<Strptr1> s =
strptr_free s
implement
array_uninitize$clear<Strptr1> (i, s) =
strptr_free s
implement
fprint_ref<Strptr1> (outf, s) =
fprint! (outf, $UN.strptr2string s)
implement (* A demonstration with linear strings. *)
main0 () =
let
#define N 10
val data =
$list_vt{Strptr1}
(string0_copy "a", string0_copy "c", string0_copy "b",
string0_copy "e", string0_copy "a", string0_copy "a",
string0_copy "d", string0_copy "d", string0_copy "b",
string0_copy "c")
var arr : @[Strptr1][N]
val () = array_copy_from_list_vt<Strptr1> (arr, data)
prval pf_arr = view@ arr
val p_arr = addr@ arr
val [m : int]
@(pf_uniq, pf_abandoned | m) =
array_remove_dups<Strptr1> (pf_arr | p_arr, i2sz N)
val () = fprint_array_sep<Strptr1> (stdout_ref, !p_arr, m, " ")
val () = println! ()
val () = array_uninitize<Strptr1> (!p_arr, m)
prval () = view@ arr :=
array_v_unsplit (pf_uniq, pf_abandoned)
in
end
(*------------------------------------------------------------------*)

View file

@ -0,0 +1,79 @@
(* Remove duplicate elements.
The elements are sorted and then only unique values are kept. *)
#include "share/atspre_staload.hats"
(* How the remove_dups template function will be called. *)
extern fn {a : t@ype}
remove_dups
{n : int}
(lt : (a, a) -<cloref> bool, (* "less than" *)
eq : (a, a) -<cloref> bool, (* "equals" *)
src : arrayref (a, n),
n : size_t n,
dst : arrayref (a, n),
m : &size_t? >> size_t m)
: #[m : nat | m <= n]
void
implement {a}
remove_dups {n} (lt, eq, src, n, dst, m) =
if n = i2sz 0 then
m := i2sz 0
else
let
prval () = lemma_arrayref_param src (* Prove 0 <= n. *)
(* Sort a copy of src. *)
val arr = arrayptr_refize (arrayref_copy (src, n))
implement array_quicksort$cmp<a> (x, y) =
if x \lt y then ~1 else 1
val () = arrayref_quicksort<a> (arr, n)
(* Copy only the first element of each run of equal elements. *)
val () = dst[0] := arr[0]
fun
loop {i : int | 1 <= i; i <= n}
{j : int | 1 <= j; j <= i}
.<n - i>.
(i : size_t i,
j : size_t j)
: [m : int | 1 <= m; m <= n]
size_t m =
if i = n then
j
else if arr[pred i] \eq arr[i] then
loop (succ i, j)
else
begin
dst[j] := arr[i];
loop (succ i, succ j)
end
val () = m := loop (i2sz 1, i2sz 1)
in
end
implement (* A demonstration. *)
main0 () =
let
val src =
arrayref_make_list<string>
(10, $list ("a", "c", "b", "e", "a",
"a", "d", "d", "b", "c"))
val dst = arrayref_make_elt<string> (i2sz 10, "?")
var m : size_t
in
remove_dups<string> (lam (x, y) => x < y,
lam (x, y) => x = y,
src, i2sz 10, dst, m);
let
prval [m : int] EQINT () = eqint_make_guint m
var i : natLte m
in
for (i := 0; i2sz i <> m; i := succ i)
print! (" ", dst[i]);
println! ()
end
end

View file

@ -0,0 +1,363 @@
(* Remove duplicate elements.
The best sorting algorithms, it is said, are O(n log n) and require
an order predicate.
But this is true only for a general sorting routine. A radix sort
for fixed-size integers is O(n), and requires no order predicate.
Here I use such a radix sort. *)
#include "share/atspre_staload.hats"
staload UN = "prelude/SATS/unsafe.sats"
(* How the remove_dups template function will be called. *)
extern fn {tk : tkind}
remove_dups
{n : int}
(src : arrayref (g0uint tk, n),
n : size_t n,
dst : arrayref (g0uint tk, n),
m : &size_t? >> size_t m)
: #[m : nat | m <= n]
void
(*------------------------------------------------------------------*)
(* A radix sort for unsigned integers, copied from my contribution to
the radix sort task. *)
extern fn {a : vt@ype}
{tk : tkind}
g0uint_radix_sort
{n : int}
(arr : &array (a, n) >> _,
n : size_t n)
:<!wrt> void
extern fn {a : vt@ype}
{tk : tkind}
g0uint_radix_sort$key
{n : int}
{i : nat | i < n}
(arr : &RD(array (a, n)),
i : size_t i)
:<> g0uint tk
fn {}
bin_sizes_to_indices
(bin_indices : &array (size_t, 256) >> _)
:<!wrt> void =
let
fun
loop {i : int | i <= 256}
{accum : int}
.<256 - i>.
(bin_indices : &array (size_t, 256) >> _,
i : size_t i,
accum : size_t accum)
:<!wrt> void =
if i <> i2sz 256 then
let
prval () = lemma_g1uint_param i
val elem = bin_indices[i]
in
if elem = i2sz 0 then
loop (bin_indices, succ i, accum)
else
begin
bin_indices[i] := accum;
loop (bin_indices, succ i, accum + g1ofg0 elem)
end
end
in
loop (bin_indices, i2sz 0, i2sz 0)
end
fn {a : vt@ype}
{tk : tkind}
count_entries
{n : int}
{shift : nat}
(arr : &RD(array (a, n)),
n : size_t n,
bin_indices : &array (size_t?, 256)
>> array (size_t, 256),
all_expended : &bool? >> bool,
shift : int shift)
:<!wrt> void =
let
fun
loop {i : int | i <= n}
.<n - i>.
(arr : &RD(array (a, n)),
bin_indices : &array (size_t, 256) >> _,
all_expended : &bool >> bool,
i : size_t i)
:<!wrt> void =
if i <> n then
let
prval () = lemma_g1uint_param i
val key : g0uint tk = g0uint_radix_sort$key<a><tk> (arr, i)
val key_shifted = key >> shift
val digit = ($UN.cast{uint} key_shifted) land 255U
val [digit : int] digit = g1ofg0 digit
extern praxi set_range :
() -<prf> [0 <= digit; digit <= 255] void
prval () = set_range ()
val count = bin_indices[digit]
val () = bin_indices[digit] := succ count
in
all_expended := all_expended * iseqz key_shifted;
loop (arr, bin_indices, all_expended, succ i)
end
prval () = lemma_array_param arr
in
array_initize_elt<size_t> (bin_indices, i2sz 256, i2sz 0);
all_expended := true;
loop (arr, bin_indices, all_expended, i2sz 0)
end
fn {a : vt@ype}
{tk : tkind}
sort_by_digit
{n : int}
{shift : nat}
(arr1 : &RD(array (a, n)),
arr2 : &array (a, n) >> _,
n : size_t n,
all_expended : &bool? >> bool,
shift : int shift)
:<!wrt> void =
let
var bin_indices : array (size_t, 256)
in
count_entries<a><tk> (arr1, n, bin_indices, all_expended, shift);
if all_expended then
()
else
let
fun
rearrange {i : int | i <= n}
.<n - i>.
(arr1 : &RD(array (a, n)),
arr2 : &array (a, n) >> _,
bin_indices : &array (size_t, 256) >> _,
i : size_t i)
:<!wrt> void =
if i <> n then
let
prval () = lemma_g1uint_param i
val key = g0uint_radix_sort$key<a><tk> (arr1, i)
val key_shifted = key >> shift
val digit = ($UN.cast{uint} key_shifted) land 255U
val [digit : int] digit = g1ofg0 digit
extern praxi set_range :
() -<prf> [0 <= digit; digit <= 255] void
prval () = set_range ()
val [j : int] j = g1ofg0 bin_indices[digit]
(* One might wish to get rid of this assertion somehow,
to eliminate the branch, should it prove a
problem. *)
val () = $effmask_exn assertloc (j < n)
val p_dst = ptr_add<a> (addr@ arr2, j)
and p_src = ptr_add<a> (addr@ arr1, i)
val _ = $extfcall (ptr, "memcpy", p_dst, p_src,
sizeof<a>)
val () = bin_indices[digit] := succ (g0ofg1 j)
in
rearrange (arr1, arr2, bin_indices, succ i)
end
prval () = lemma_array_param arr1
in
bin_sizes_to_indices<> bin_indices;
rearrange (arr1, arr2, bin_indices, i2sz 0)
end
end
fn {a : vt@ype}
{tk : tkind}
g0uint_sort {n : pos}
(arr1 : &array (a, n) >> _,
arr2 : &array (a, n) >> _,
n : size_t n)
:<!wrt> void =
let
fun
loop {idigit_max, idigit : nat | idigit <= idigit_max}
.<idigit_max - idigit>.
(arr1 : &array (a, n) >> _,
arr2 : &array (a, n) >> _,
from1to2 : bool,
idigit_max : int idigit_max,
idigit : int idigit)
:<!wrt> void =
if idigit = idigit_max then
begin
if ~from1to2 then
let
val _ =
$extfcall (ptr, "memcpy", addr@ arr1, addr@ arr2,
sizeof<a> * n)
in
end
end
else if from1to2 then
let
var all_expended : bool
in
sort_by_digit<a><tk> (arr1, arr2, n, all_expended,
8 * idigit);
if all_expended then
()
else
loop (arr1, arr2, false, idigit_max, succ idigit)
end
else
let
var all_expended : bool
in
sort_by_digit<a><tk> (arr2, arr1, n, all_expended,
8 * idigit);
if all_expended then
let
val _ =
$extfcall (ptr, "memcpy", addr@ arr1, addr@ arr2,
sizeof<a> * n)
in
end
else
loop (arr1, arr2, true, idigit_max, succ idigit)
end
in
loop (arr1, arr2, true, sz2i sizeof<g1uint tk>, 0)
end
#define SIZE_THRESHOLD 256
extern praxi
unsafe_cast_array
{a : vt@ype}
{b : vt@ype}
{n : int}
(arr : &array (b, n) >> array (a, n))
:<prf> void
implement {a} {tk}
g0uint_radix_sort {n} (arr, n) =
if n <> 0 then
let
prval () = lemma_array_param arr
fn
sort {n : pos}
(arr1 : &array (a, n) >> _,
arr2 : &array (a, n) >> _,
n : size_t n)
:<!wrt> void =
g0uint_sort<a><tk> (arr1, arr2, n)
in
if n <= SIZE_THRESHOLD then
let
var arr2 : array (a, SIZE_THRESHOLD)
prval @(pf_left, pf_right) =
array_v_split {a?} {..} {SIZE_THRESHOLD} {n} (view@ arr2)
prval () = view@ arr2 := pf_left
prval () = unsafe_cast_array{a} arr2
val () = sort (arr, arr2, n)
prval () = unsafe_cast_array{a?} arr2
prval () = view@ arr2 :=
array_v_unsplit (view@ arr2, pf_right)
in
end
else
let
val @(pf_arr2, pfgc_arr2 | p_arr2) = array_ptr_alloc<a> n
macdef arr2 = !p_arr2
prval () = unsafe_cast_array{a} arr2
val () = sort (arr, arr2, n)
prval () = unsafe_cast_array{a?} arr2
val () = array_ptr_free (pf_arr2, pfgc_arr2 | p_arr2)
in
end
end
(*------------------------------------------------------------------*)
(* An implementation of the remove_dups template function, which also
sorts the elements. *)
implement {tk}
remove_dups {n} (src, n, dst, m) =
if n = i2sz 0 then
m := i2sz 0
else
let
prval () = lemma_arrayref_param src (* Prove 0 <= n. *)
(* Sort a copy of src. *)
val arrptr = arrayref_copy (src, n)
val @(pf_arr | p_arr) = arrayptr_takeout_viewptr arrptr
val () = g0uint_radix_sort<g0uint tk><tk> (!p_arr, n)
prval () = arrayptr_addback (pf_arr | arrptr)
(* Copy only the first element of each run of equals. *)
val () = dst[0] := arrptr[0]
fun
loop {i : int | 1 <= i; i <= n}
{j : int | 1 <= j; j <= i}
.<n - i>.
(arrptr : !arrayptr (g0uint tk, n),
i : size_t i,
j : size_t j)
: [m : int | 1 <= m; m <= n]
size_t m =
if i = n then
j
else if arrptr[pred i] = arrptr[i] then
loop (arrptr, succ i, j)
else
begin
dst[j] := arrptr[i];
loop (arrptr, succ i, succ j)
end
val () = m := loop (arrptr, i2sz 1, i2sz 1)
val () = arrayptr_free arrptr
in
end
(*------------------------------------------------------------------*)
(* A demonstration. *)
implement
main0 () =
let
implement
g0uint_radix_sort$key<uint><uintknd> (arr, i) =
arr[i]
val src =
arrayref_make_list<uint>
(10, $list (1U, 3U, 2U, 5U, 1U, 1U, 4U, 4U, 2U, 3U))
val dst = arrayref_make_elt<uint> (i2sz 10, 123456789U)
var m : size_t
in
remove_dups<uintknd> (src, i2sz 10, dst, m);
let
prval [m : int] EQINT () = eqint_make_guint m
var i : natLte m
in
for (i := 0; i2sz i <> m; i := succ i)
print! (" ", dst[i]);
println! ()
end
end
(*------------------------------------------------------------------*)

View file

@ -0,0 +1,86 @@
(* Remove duplicate elements.
Elements already seen are put into a hash table. *)
#include "share/atspre_staload.hats"
(* Use hash tables from the libats/ML library. *)
staload "libats/ML/SATS/hashtblref.sats"
staload _ = "libats/ML/DATS/hashtblref.dats"
staload _ = "libats/DATS/hashfun.dats"
staload _ = "libats/DATS/hashtbl_chain.dats"
staload _ = "libats/DATS/linmap_list.dats"
(* How the remove_dups template function will be called. *)
extern fn {key, a : t@ype}
remove_dups
{n : int}
(key : a -<cloref> key,
src : arrayref (a, n),
n : size_t n,
dst : arrayref (a, n),
m : &size_t? >> size_t m)
: #[m : nat | m <= n]
void
implement {key, a}
remove_dups {n} (key, src, n, dst, m) =
if n = i2sz 0 then
m := i2sz 0
else
let
prval () = lemma_arrayref_param src (* Prove 0 <= n. *)
fun
loop {i : nat | i <= n}
{j : nat | j <= i}
.<n - i>.
(ht : hashtbl (key, a),
i : size_t i,
j : size_t j)
: [m : nat | m <= n]
size_t m =
if i = n then
j
else
let
val x = src[i]
val k = key x
in
case+ hashtbl_search<key, a> (ht, k) of
| ~ None_vt () =>
begin (* An element not yet encountered. Copy it. *)
hashtbl_insert_any<key, a> (ht, k, x);
dst[j] := x;
loop (ht, succ i, succ j)
end
| ~ Some_vt _ =>
begin (* An element already encountered. Skip it. *)
loop (ht, succ i, j)
end
end;
in
m := loop (hashtbl_make_nil<key, a> (i2sz 1024),
i2sz 0, i2sz 0)
end
implement (* A demonstration. *)
main0 () =
let
val src =
arrayref_make_list<string>
(10, $list ("a", "c", "b", "e", "a",
"a", "d", "d", "b", "c"))
val dst = arrayref_make_elt<string> (i2sz 10, "?")
var m : size_t
in
remove_dups<string, string> (lam s => s, src, i2sz 10, dst, m);
let
prval [m : int] EQINT () = eqint_make_guint m
var i : natLte m
in
for (i := 0; i2sz i <> m; i := succ i)
print! (" ", dst[i]);
println! ()
end
end

View file

@ -0,0 +1,77 @@
(* Remove duplicate elements.
This implementation is for elements that contain a "this has been
seen" flag. It is O(n) in the number of elements.
Also, this implementation demonstrates that imperative programming,
without dependent types or proofs, is possible in ATS. *)
#include "share/atspre_staload.hats"
(* A tuple in the heap. *)
typedef seen_or_not (a : t@ype+) = '(a, ref bool)
(* How the remove_dups function will be called. *)
extern fn {a : t@ype}
remove_dups
(given_data : arrszref (seen_or_not a),
space_for_result : arrszref (seen_or_not a),
num_of_unique_elems : &size_t? >> size_t)
: void
implement {a}
remove_dups (given_data, space_for_result, num_of_unique_elems) =
let
macdef seen (i) = given_data[,(i)].1
var i : size_t
var j : size_t
in
(* Clear all the "seen" flags. *)
for (i := i2sz 0; i <> size given_data; i := succ i)
!(seen i) := false;
(* Loop through given_data, copying (pointers to) any values that
have not yet been seen. *)
j := i2sz 0;
for (i := i2sz 0; i <> size given_data; i := succ i)
if !(seen i) then
() (* Skip any element that has already been seen. *)
else
begin
!(seen i) := true; (* Mark the element as seen. *)
space_for_result[j] := given_data[i];
j := succ j
end;
num_of_unique_elems := j
end
implement (* A demonstration. *)
main0 () =
let
(* Define some values. *)
val a = '("a", ref<bool> false)
val b = '("b", ref<bool> false)
val c = '("c", ref<bool> false)
val d = '("d", ref<bool> false)
val e = '("e", ref<bool> false)
(* Fill an array with values. *)
val data =
arrszref_make_list ($list (a, c, b, e, a, a, d, d, b, c))
(* Allocate storage for the result. *)
val unique_elems = arrszref_make_elt (i2sz 10, a)
var num_of_unique_elems : size_t
var i : size_t
in
(* Remove duplicates. *)
remove_dups<string> (data, unique_elems, num_of_unique_elems);
(* Print the results. *)
for (i := i2sz 0; i <> num_of_unique_elems; i := succ i)
print! (" ", unique_elems[i].0);
println! ()
end

View file

@ -0,0 +1,2 @@
$ 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

View file

@ -0,0 +1,60 @@
INCLUDE "D2:SORT.ACT" ;from the Action! Tool Kit
PROC PrintArray(INT ARRAY a INT size)
INT i
Put('[)
FOR i=0 TO size-1
DO
IF i>0 THEN Put(' ) FI
PrintI(a(i))
OD
Put(']) PutE()
RETURN
PROC RemoveDuplicates(INT ARRAY src INT srcLen
INT ARRAY dst INT POINTER dstLen)
INT i
CHAR curr,prev
IF srcLen=0 THEN
dstLen^=0
RETURN
FI
SortI(src,srcLen,0)
dst(0)=src(0)
dstLen^=1 prev=src(0)
FOR i=1 TO srcLen-1
DO
curr=src(i)
IF curr#prev THEN
dst(dstLen^)=curr
dstLen^==+1
FI
prev=curr
OD
RETURN
PROC Test(INT ARRAY src INT srcLen)
INT ARRAY dst(100)
INT dstLen
PrintE("Input array:")
PrintArray(src,srcLen)
RemoveDuplicates(src,srcLen,dst,@dstLen)
PrintE("Unique items:")
PrintArray(dst,dstLen)
PutE()
RETURN
PROC Main()
INT ARRAY src1(9)=[1 3 65534 0 12 1 65534 52 3]
INT ARRAY src2(26)=[3 2 1 3 2 5 2 1 6 3 4 2 5 3 1 5 3 5 2 1 3 7 4 5 7 6]
INT ARRAY src3(1)=[6502]
Put(125) PutE() ;clear screen
Test(src1,9)
Test(src2,26)
Test(src3,1)
RETURN

View file

@ -0,0 +1,15 @@
with Ada.Containers.Ordered_Sets, Ada.Text_IO;
use Ada.Text_IO;
procedure Duplicate is
package Int_Sets is new Ada.Containers.Ordered_Sets (Integer);
Nums : constant array (Natural range <>) of Integer := (1,2,3,4,5,5,6,7,1);
Unique : Int_Sets.Set;
begin
for n of Nums loop
Unique.Include (n);
end loop;
for e of Unique loop
Put (e'img);
end loop;
end Duplicate;

View file

@ -0,0 +1,5 @@
index x;
list(1, 2, 3, 1, 2, 3, 4, 1).ucall(i_add, 1, x, 0);
x.i_vcall(o_, 1, " ");
o_newline();

View file

@ -0,0 +1,8 @@
index x;
for (, integer a in list(8, 2, 1, 8, 2, 1, 4, 8)) {
if ((x[a] += 1) == 1) {
o_(" ", a);
}
}
o_newline();

View file

@ -0,0 +1,18 @@
#include <hopper.h>
main:
x=-1
{30} rand array (x), mulby(10), ceil, mov(x)
{"Original Array:\n",x}, println
{x}array(SORT),
{x}sets(UNIQUE), mov(x)
{"Final array:\n",x}, println
y={}
{"C","Go","Go","C","Cobol","java","Ada"} pushall(y)
{"java","algol-68","C","java","fortran"} pushall(y)
{"\nOriginal Array:\n",y}, println
{y}array(SORT),
{y}sets(UNIQUE), mov(y)
{"Final array:\n",y}, println
exit(0)

View file

@ -0,0 +1,9 @@
unique({1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d"})
on unique(x)
set R to {}
repeat with i in x
if i is not in R then set end of R to i's contents
end repeat
return R
end unique

View file

@ -0,0 +1,3 @@
{1, 2, 3, {4, 5}} contains {2, 3} --> true
{1, 2, 3, {4, 5}} contains {4, 5} --> false
{1, 2, 3, {4, 5}} contains {{4, 5}} --> true

View file

@ -0,0 +1 @@
3 is in {1, 2, 3, {4, 5}} --> {3} is in {1, 2, 3, {4, 5}} --> true

View file

@ -0,0 +1,10 @@
unique({1, 2, 3, "a", "b", "c", 2, 3, 4, "c", {b:"c"}, {"c"}, "c", "d"})
on unique(x)
set R to {}
repeat with i in x
set i to i's contents
if {i} is not in R then set end of R to i
end repeat
return R
end unique

View file

@ -0,0 +1 @@
{1, 2, 3, "a", "b", "c", 4, {b:"c"}, {"c"}, "d"}

View file

@ -0,0 +1,92 @@
-- CASE-INSENSITIVE UNIQUE ELEMENTS ------------------------------------------
-- nub :: [a] -> [a]
on nub(xs)
-- Eq :: a -> a -> Bool
script Eq
on |λ|(x, y)
ignoring case
x = y
end ignoring
end |λ|
end script
nubBy(Eq, xs)
end nub
-- TEST ----------------------------------------------------------------------
on run
{intercalate(space, ¬
nub(splitOn(space, "4 3 2 8 0 1 9 5 1 7 6 3 9 9 4 2 1 5 3 2"))), ¬
intercalate("", ¬
nub(characters of "abcabc ABCABC"))}
--> {"4 3 2 8 0 1 9 5 7 6", "abc "}
end run
-- GENERIC FUNCTIONS ---------------------------------------------------------
-- filter :: (a -> Bool) -> [a] -> [a]
on filter(f, xs)
tell mReturn(f)
set lst to {}
set lng to length of xs
repeat with i from 1 to lng
set v to item i of xs
if |λ|(v, i, xs) then set end of lst to v
end repeat
return lst
end tell
end filter
-- intercalate :: Text -> [Text] -> Text
on intercalate(strText, lstText)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
set my text item delimiters to dlm
return strJoined
end intercalate
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- nubBy :: (a -> a -> Bool) -> [a] -> [a]
on nubBy(fnEq, xxs)
set lng to length of xxs
if lng > 1 then
set x to item 1 of xxs
set xs to items 2 thru -1 of xxs
set p to mReturn(fnEq)
-- notEq :: a -> Bool
script notEq
on |λ|(a)
not (p's |λ|(a, x))
end |λ|
end script
{x} & nubBy(fnEq, filter(notEq, xs))
else
xxs
end if
end nubBy
-- splitOn :: Text -> Text -> [Text]
on splitOn(strDelim, strMain)
set {dlm, my text item delimiters} to {my text item delimiters, strDelim}
set lstParts to text items of strMain
set my text item delimiters to dlm
return lstParts
end splitOn

View file

@ -0,0 +1 @@
{"4 3 2 8 0 1 9 5 7 6", "abc "}

View file

@ -0,0 +1,6 @@
use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
set aList to {1, 2, 3, "a", "b", "c", 2, 3, 4, "c", {b:"c"}, {"c"}, "c", "d"}
set orderedSet to current application's class "NSOrderedSet"'s orderedSetWithArray:(aList)
return orderedSet's array() as list

View file

@ -0,0 +1 @@
{1, 2, 3, "a", "b", "c", 4, {b:"c"}, {"c"}, "d"}

View file

@ -0,0 +1,41 @@
100 DIM L$(15)
110 L$(0) = "NOW"
120 L$(1) = "IS"
130 L$(2) = "THE"
140 L$(3) = "TIME"
150 L$(4) = "FOR"
160 L$(5) = "ALL"
170 L$(6) = "GOOD"
180 L$(7) = "MEN"
190 L$(8) = "TO"
200 L$(9) = "COME"
210 L$(10) = "TO"
220 L$(11) = "THE"
230 L$(12) = "AID"
240 L$(13) = "OF"
250 L$(14) = "THE"
260 L$(15) = "PARTY."
300 N = 15
310 GOSUB 400
320 FOR I = 0 TO N
330 PRINT L$(I) " " ;
340 NEXT
350 PRINT
360 END
400 REMREMOVE DUPLICATES
410 FOR I = N TO 1 STEP -1
420 I$ = L$(I)
430 FOR J = 0 TO I - 1
440 EQ = I$ = L$(J)
450 IF NOT EQ THEN NEXT J
460 IF EQ THEN GOSUB 500
470 NEXT I
480 RETURN
500 REMREMOVE ELEMENT
510 L$(I) = L$(N)
520 L$(N) = ""
530 N = N - 1
540 RETURN

View file

@ -0,0 +1,3 @@
arr: [1 2 3 2 1 2 3 4 5 3 2 1]
print unique arr

View file

@ -0,0 +1,3 @@
a = 1,2,1,4,5,2,15,1,3,4
Sort, a, a, NUD`,
MsgBox % a ; 1,2,3,4,5,15

View file

@ -0,0 +1,28 @@
arraybase 1
max = 10
dim res(max)
dim dat(max)
dat[1] = 1: dat[2] = 2: dat[3] = 1: dat[4] = 4: dat[5] = 5
dat[6] = 2: dat[7] = 15: dat[8] = 1: dat[9] = 3: dat[10] = 4
res[1] = dat[1]
cont = 1
posic = 1
while posic < max
posic += 1
esnuevo = 1
indice = 1
while indice <= cont and esnuevo = 1
if dat[posic] = res[indice] then esnuevo = 0
indice += 1
end while
if esnuevo = 1 then
cont += 1
res[cont] = dat[posic]
end if
end while
for i = 1 to cont
print res[i]; " ";
next i
end

View file

@ -0,0 +1,21 @@
DIM list$(15)
list$() = "Now", "is", "the", "time", "for", "all", "good", "men", \
\ "to", "come", "to", "the", "aid", "of", "the", "party."
num% = FNremoveduplicates(list$())
FOR i% = 0 TO num%-1
PRINT list$(i%) " " ;
NEXT
PRINT
END
DEF FNremoveduplicates(l$())
LOCAL i%, j%, n%, i$
n% = 1
FOR i% = 1 TO DIM(l$(), 1)
i$ = l$(i%)
FOR j% = 0 TO i%-1
IF i$ = l$(j%) EXIT FOR
NEXT
IF j%>=i% l$(n%) = i$ : n% += 1
NEXT
= n%

View file

@ -0,0 +1,63 @@
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
&
)

View file

@ -0,0 +1,3 @@
some_array = [1 1 2 1 'redundant' [1 2 3] [1 2 3] 'redundant']
unique_array = some_array.unique

View file

@ -0,0 +1,15 @@
#include <set>
#include <iostream>
using namespace std;
int main() {
typedef set<int> TySet;
int data[] = {1, 2, 3, 2, 3, 4};
TySet unique_set(data, data + 6);
cout << "Set items:" << endl;
for (TySet::iterator iter = unique_set.begin(); iter != unique_set.end(); iter++)
cout << *iter << " ";
cout << endl;
}

View file

@ -0,0 +1,15 @@
#include <ext/hash_set>
#include <iostream>
using namespace std;
int main() {
typedef __gnu_cxx::hash_set<int> TyHash;
int data[] = {1, 2, 3, 2, 3, 4};
TyHash unique_set(data, data + 6);
cout << "Set items:" << endl;
for (TyHash::iterator iter = unique_set.begin(); iter != unique_set.end(); iter++)
cout << *iter << " ";
cout << endl;
}

View file

@ -0,0 +1,15 @@
#include <tr1/unordered_set>
#include <iostream>
using namespace std;
int main() {
typedef tr1::unordered_set<int> TyHash;
int data[] = {1, 2, 3, 2, 3, 4};
TyHash unique_set(data, data + 6);
cout << "Set items:" << endl;
for (TyHash::iterator iter = unique_set.begin(); iter != unique_set.end(); iter++)
cout << *iter << " ";
cout << endl;
}

View file

@ -0,0 +1,15 @@
#include <iostream>
#include <iterator>
#include <algorithm>
// helper template
template<typename T> T* end(T (&array)[size]) { return array+size; }
int main()
{
int data[] = { 1, 2, 3, 2, 3, 4 };
std::sort(data, end(data));
int* new_end = std::unique(data, end(data));
std::copy(data, new_end, std::ostream_iterator<int>(std::cout, " ");
std::cout << std::endl;
}

View file

@ -0,0 +1,14 @@
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> data = {1, 2, 3, 2, 3, 4};
std::sort(data.begin(), data.end());
data.erase(std::unique(data.begin(), data.end()), data.end());
for(int& i: data) std::cout << i << " ";
std::cout << std::endl;
return 0;
}

View file

@ -0,0 +1,5 @@
int[] nums = { 1, 1, 2, 3, 4, 4 };
List<int> unique = new List<int>();
foreach (int n in nums)
if (!unique.Contains(n))
unique.Add(n);

View file

@ -0,0 +1,2 @@
int[] nums = {1, 1, 2, 3, 4, 4};
int[] unique = nums.Distinct().ToArray();

View file

@ -0,0 +1,33 @@
#include <stdio.h>
#include <stdlib.h>
struct list_node {int x; struct list_node *next;};
typedef struct list_node node;
node * uniq(int *a, unsigned alen)
{if (alen == 0) return NULL;
node *start = malloc(sizeof(node));
if (start == NULL) exit(EXIT_FAILURE);
start->x = a[0];
start->next = NULL;
for (int i = 1 ; i < alen ; ++i)
{node *n = start;
for (;; n = n->next)
{if (a[i] == n->x) break;
if (n->next == NULL)
{n->next = malloc(sizeof(node));
n = n->next;
if (n == NULL) exit(EXIT_FAILURE);
n->x = a[i];
n->next = NULL;
break;}}}
return start;}
int main(void)
{int a[] = {1, 2, 1, 4, 5, 2, 15, 1, 3, 4};
for (node *n = uniq(a, 10) ; n != NULL ; n = n->next)
printf("%d ", n->x);
puts("");
return 0;}

View file

@ -0,0 +1,59 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
/* Returns `true' if element `e' is in array `a'. Otherwise, returns `false'.
* Checks only the first `n' elements. Pure, O(n).
*/
bool elem(int *a, size_t n, int e)
{
for (size_t i = 0; i < n; ++i)
if (a[i] == e)
return true;
return false;
}
/* Removes the duplicates in array `a' of given length `n'. Returns the number
* of unique elements. In-place, order preserving, O(n ^ 2).
*/
size_t nub(int *a, size_t n)
{
size_t m = 0;
for (size_t i = 0; i < n; ++i)
if (!elem(a, m, a[i]))
a[m++] = a[i];
return m;
}
/* Out-place version of `nub'. Pure, order preserving, alloc < n * sizeof(int)
* bytes, O(n ^ 2).
*/
size_t nub_new(int **b, int *a, size_t n)
{
int *c = malloc(n * sizeof(int));
memcpy(c, a, n * sizeof(int));
int m = nub(c, n);
*b = malloc(m * sizeof(int));
memcpy(*b, c, m * sizeof(int));
free(c);
return m;
}
int main(void)
{
int a[] = {1, 2, 1, 4, 5, 2, 15, 1, 3, 4};
int *b;
size_t n = nub_new(&b, a, sizeof(a) / sizeof(a[0]));
for (size_t i = 0; i < n; ++i)
printf("%d ", b[i]);
puts("");
free(b);
return 0;
}

View file

@ -0,0 +1,29 @@
#include <stdio.h>
#include <stdlib.h>
int icmp(const void *a, const void *b)
{
#define _I(x) *(const int*)x
return _I(a) < _I(b) ? -1 : _I(a) > _I(b);
#undef _I
}
/* filter items in place and return number of uniques. if a separate
list is desired, duplicate it before calling this function */
int uniq(int *a, int len)
{
int i, j;
qsort(a, len, sizeof(int), icmp);
for (i = j = 0; i < len; i++)
if (a[i] != a[j]) a[++j] = a[i];
return j + 1;
}
int main()
{
int x[] = {1, 2, 1, 4, 5, 2, 15, 1, 3, 4};
int i, len = uniq(x, sizeof(x) / sizeof(x[0]));
for (i = 0; i < len; i++) printf("%d\n", x[i]);
return 0;
}

View file

@ -0,0 +1,2 @@
red (1 2 3 4) (3 2 1 5) .
--> (4 5 1 2 3):Int

View file

@ -0,0 +1,19 @@
mod! NO-DUP-LIST(ELEMENTS :: TRIV) {
op __ : Elt Elt -> Elt { comm assoc idem assoc }
}
-- Runs on Version 1.5.1(PigNose0.99) of CafeOBJ
-- The tests are performed after opening instantiated NO-DUP-LIST with various concrete types.
-- Test on lists of INT
open NO-DUP-LIST(INT) .
red 2 1 2 1 2 1 3 .
-- Gives (2 1 3):Int
open NO-DUP-LIST(INT) .
reduce 1 1 2 1 1 .
close
open NO-DUP-LIST(CHARACTER) .
reduce 'a' 'b' 'a' 'a' .
close
open NO-DUP-LIST(STRING) .
reduce "abc" "def" "abc" "abc" "abc" .
close

View file

@ -0,0 +1,2 @@
<String|Integer>[] data = [1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d"];
<String|Integer>[] unique = HashSet { *data }.sequence();

View file

@ -0,0 +1,3 @@
user=> (distinct [1 3 2 9 1 2 3 8 8 1 0 2])
(1 3 2 9 8 0)
user=>

View file

@ -0,0 +1,6 @@
data = [ 1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d" ]
set = []
set.push i for i in data when not (i in set)
console.log data
console.log set

View file

@ -0,0 +1,2 @@
(remove-duplicates '(1 3 2 9 1 2 3 8 8 1 0 2))
> (9 3 8 1 0 2)

View file

@ -0,0 +1,2 @@
(delete-duplicates '(1 3 2 9 1 2 3 8 8 1 0 2))
> (9 3 8 1 0 2)

View file

@ -0,0 +1,2 @@
ary = [1, 1, 2, 2, "a", [1, 2, 3], [1, 2, 3], "a"]
p ary.uniq

View file

@ -0,0 +1,8 @@
void main() {
import std.stdio, std.algorithm;
[1, 3, 2, 9, 1, 2, 3, 8, 8, 1, 0, 2]
.sort()
.uniq
.writeln;
}

View file

@ -0,0 +1,10 @@
void main() {
import std.stdio;
immutable data = [1, 3, 2, 9, 1, 2, 3, 8, 8, 1, 0, 2];
bool[int] hash;
foreach (el; data)
hash[el] = true;
hash.byKey.writeln;
}

View file

@ -0,0 +1,7 @@
void main()
{
import std.stdio, std.algorithm, std.array;
auto a = [5,4,32,7,6,4,2,6,0,8,6,9].sort.uniq.array;
a.writeln;
}

View file

@ -0,0 +1,24 @@
program RemoveDuplicateElements;
{$APPTYPE CONSOLE}
uses Generics.Collections;
var
i: Integer;
lIntegerList: TList<Integer>;
const
INT_ARRAY: array[1..7] of Integer = (1, 2, 2, 3, 4, 5, 5);
begin
lIntegerList := TList<Integer>.Create;
try
for i in INT_ARRAY do
if not lIntegerList.Contains(i) then
lIntegerList.Add(i);
for i in lIntegerList do
Writeln(i);
finally
lIntegerList.Free;
end;
end.

View file

@ -0,0 +1 @@
[1,2,3,2,3,4].asSet().getElements()

View file

@ -0,0 +1,2 @@
inNumbers := DATASET([{1},{2},{3},{4},{1},{1},{7},{8},{9},{9},{0},{0},{3},{3},{3},{3},{3}], {INTEGER Field1});
DEDUP(SORT(inNumbers,Field1));

View file

@ -0,0 +1,14 @@
a[] = [ 1 2 1 4 5 2 15 1 3 4 ]
for a in a[]
found = 0
for b in b[]
if a = b
found = 1
break 1
.
.
if found = 0
b[] &= a
.
.
print b[]

View file

@ -0,0 +1,9 @@
module RetainUniqueValues {
void run() {
Int[] array = [1, 2, 3, 2, 1, 2, 3, 4, 5, 3, 2, 1];
array = array.distinct().toArray();
@Inject Console console;
console.print($"result={array}");
}
}

View file

@ -0,0 +1,13 @@
import extensions;
import system'collections;
import system'routines;
public program()
{
var nums := new int[]{1,1,2,3,4,4};
auto unique := new Map<int, int>();
nums.forEach:(n){ unique[n] := n };
console.printLine(unique.MapValues.asEnumerable())
}

View file

@ -0,0 +1,27 @@
defmodule RC do
# Set approach
def uniq1(list), do: MapSet.new(list) |> MapSet.to_list
# Sort approach
def uniq2(list), do: Enum.sort(list) |> Enum.dedup
# Go through the list approach
def uniq3(list), do: uniq3(list, [])
defp uniq3([], res), do: Enum.reverse(res)
defp uniq3([h|t], res) do
if h in res, do: uniq3(t, res), else: uniq3(t, [h | res])
end
end
num = 10000
max = div(num, 10)
list = for _ <- 1..num, do: :rand.uniform(max)
funs = [&Enum.uniq/1, &RC.uniq1/1, &RC.uniq2/1, &RC.uniq3/1]
Enum.each(funs, fn fun ->
result = fun.([1,1,2,1,'redundant',1.0,[1,2,3],[1,2,3],'redundant',1.0])
:timer.tc(fn ->
Enum.each(1..100, fn _ -> fun.(list) end)
end)
|> fn{t,_} -> IO.puts "#{inspect fun}:\t#{t/1000000}\t#{inspect result}" end.()
end)

View file

@ -0,0 +1,4 @@
List = [1, 2, 3, 2, 2, 4, 5, 5, 4, 6, 6, 5].
UniqueList = gb_sets:to_list(gb_sets:from_list(List)).
% Alternatively the builtin:
Unique_list = lists:usort( List ).

View file

@ -0,0 +1,17 @@
include sort.e
function uniq(sequence s)
sequence out
s = sort(s)
out = s[1..1]
for i = 2 to length(s) do
if not equal(s[i],out[$]) then
out = append(out, s[i])
end if
end for
return out
end function
constant s = {1, 2, 1, 4, 5, 2, 15, 1, 3, 4}
? s
? uniq(s)

View file

@ -0,0 +1 @@
set [|1;2;3;2;3;4|]

View file

@ -0,0 +1 @@
val it : Set<int> = seq [1; 2; 3; 4]

View file

@ -0,0 +1,4 @@
USING: sets ;
V{ 1 2 1 3 2 4 5 } members .
V{ 1 2 3 4 5 }

View file

@ -0,0 +1,19 @@
\ Increments a2 until it no longer points to the same value as a1
\ a3 is the address beyond the data a2 is traversing.
: skip-dups ( a1 a2 a3 -- a1 a2+n )
dup rot ?do
over @ i @ <> if drop i leave then
cell +loop ;
\ Compress an array of cells by removing adjacent duplicates
\ Returns the new count
: uniq ( a n -- n2 )
over >r \ Original addr to return stack
cells over + >r \ "to" addr now on return stack, available as r@
dup begin ( write read )
dup r@ <
while
2dup @ swap ! \ copy one cell
cell+ r@ skip-dups
cell 0 d+ \ increment write ptr only
repeat r> 2drop r> - cell / ;

View file

@ -0,0 +1,12 @@
: uniqv { a n \ r e -- n }
a n cells+ to e
a dup to r
\ the write address lives on the stack
begin
r e <
while
r @ over !
r cell+ e skip-dups to r
cell+
repeat
a - cell / ;

View file

@ -0,0 +1,5 @@
create test 1 , 2 , 3 , 2 , 6 , 4 , 5 , 3 , 6 ,
here test - cell / constant ntest
: .test ( n -- ) 0 ?do test i cells + ? loop ;
test ntest 2dup cell-sort uniq .test

View file

@ -0,0 +1,24 @@
program remove_dups
implicit none
integer :: example(12) ! The input
integer :: res(size(example)) ! The output
integer :: k ! The number of unique elements
integer :: i, j
example = [1, 2, 3, 2, 2, 4, 5, 5, 4, 6, 6, 5]
k = 1
res(1) = example(1)
outer: do i=2,size(example)
do j=1,k
if (res(j) == example(i)) then
! Found a match so start looking again
cycle outer
end if
end do
! No match found so add it to the output
k = k + 1
res(k) = example(i)
end do outer
write(*,advance='no',fmt='(a,i0,a)') 'Unique list has ',k,' elements: '
write(*,*) res(1:k)
end program remove_dups

View file

@ -0,0 +1,21 @@
program remove_dups
implicit none
integer :: example(12) ! The input
integer :: res(size(example)) ! The output
integer :: k ! The number of unique elements
integer :: i
example = [1, 2, 3, 2, 2, 4, 5, 5, 4, 6, 6, 5]
k = 1
res(1) = example(1)
do i=2,size(example)
! if the number already exist in res check next
if (any( res == example(i) )) cycle
! No match found so add it to the output
k = k + 1
res(k) = example(i)
end do
write(*,advance='no',fmt='(a,i0,a)') 'Unique list has ',k,' elements: '
write(*,*) res(1:k)
end program remove_dups

View file

@ -0,0 +1,39 @@
' FB 1.05.0 Win64
Sub removeDuplicates(a() As Integer, b() As Integer)
Dim lb As Integer = LBound(a)
Dim ub As Integer = UBound(a)
If ub = -1 Then Return '' empty array
Redim b(lb To ub)
b(lb) = a(lb)
Dim count As Integer = 1
Dim unique As Boolean
For i As Integer = lb + 1 To ub
unique = True
For j As Integer = lb to i - 1
If a(i) = a(j) Then
unique = False
Exit For
End If
Next j
If unique Then
b(lb + count) = a(i)
count += 1
End If
Next i
If count > 0 Then Redim Preserve b(lb To lb + count - 1)
End Sub
Dim a(1 To 10) As Integer = {1, 2, 1, 4, 5, 2, 15, 1, 3, 4}
Dim b() As Integer
removeDuplicates a(), b()
For i As Integer = LBound(b) To UBound(b)
Print b(i); " ";
Next
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1,9 @@
b = [1, 5, 2, 6, 6, 2, 2, 1, 9, 8, 6, 5]
// One way, using OrderedList. An OrderedList is a type of array that keeps
// its elements in order. The items must be comparable.
a = new OrderedList
println[a.insertAllUnique[b]]
// Another way, using the "set" datatype and back to an array.
println[toArray[toSet[b]]

View file

@ -0,0 +1,10 @@
include "NSLog.incl"
CFArrayRef array, unique
OrderedSetRef ordered
array = @[@"A", @"B", @"C", @"B", @"A", @"C", @"A", @"C", @"A", @"B", @"C"]
ordered = fn OrderedSetWithArray( array )
NSLog( @"%@", fn OrderedSetArray( ordered ) )
HandleEvents

View file

@ -0,0 +1,9 @@
# Built-in, using sets (which are also lists)
a := [ 1, 2, 3, 1, [ 4 ], 5, 5, [4], 6 ];
# [ 1, 2, 3, 1, [ 4 ], 5, 5, [ 4 ], 6 ]
b := Set(a);
# [ 1, 2, 3, 5, 6, [ 4 ] ]
IsSet(b);
# true
IsList(b);
# true

View file

@ -0,0 +1,27 @@
10 ' Remove Duplicates
20 OPTION BASE 1
30 LET MAXI% = 7
40 DIM D(7), R(7): ' data, result
50 ' Set the data.
60 FOR I% = 1 TO 7
70 READ D(I%)
80 NEXT I%
90 ' Remove duplicates.
100 LET R(1) = D(1)
110 LET LRI% = 1: ' last index of result
120 LET P% = 1: ' position
130 WHILE P% < MAXI%
140 LET P% = P% + 1
150 LET ISNEW = 1: ' is a new number?
160 LET RI% = 1: ' current index of result
170 WHILE (RI% <= LRI%) AND ISNEW
180 IF D(P%) = R(RI%) THEN LET ISNEW = 0
190 LET RI% = RI% + 1
200 WEND
210 IF ISNEW THEN LET LRI% = LRI% + 1: LET R(LRI%) = D(P%)
220 WEND
230 FOR RI% = 1 TO LRI%
240 PRINT R(RI%)
250 NEXT RI%
260 END
1000 DATA 1, 2, 2, 3, 4, 5, 5

View file

@ -0,0 +1,14 @@
Public Sub Main()
Dim sString As String[] = Split("Now is the time for all the good men to come to the aid of the good party 1 2 1 3 3 3 2 1 1 2 3 4 33 2 5 4 333 5", " ")
Dim sFix As New String[]
Dim sTemp As String
For Each sTemp In sString
sTemp &= " "
If InStr(sFix.Join(" ") & " ", sTemp) Then Continue
sFix.Add(Trim(sTemp))
Next
Print sFix.Join(" ")
End

View file

@ -0,0 +1,19 @@
package main
import "fmt"
func uniq(list []int) []int {
unique_set := make(map[int]bool, len(list))
for _, x := range list {
unique_set[x] = true
}
result := make([]int, 0, len(unique_set))
for x := range unique_set {
result = append(result, x)
}
return result
}
func main() {
fmt.Println(uniq([]int{1, 2, 3, 2, 3, 4})) // prints: [3 4 1 2] (but in a semi-random order)
}

View file

@ -0,0 +1,23 @@
package main
import "fmt"
func uniq(list []int) []int {
unique_set := make(map[int]int, len(list))
i := 0
for _, x := range list {
if _, there := unique_set[x]; !there {
unique_set[x] = i
i++
}
}
result := make([]int, len(unique_set))
for x, i := range unique_set {
result[i] = x
}
return result
}
func main() {
fmt.Println(uniq([]int{1, 2, 3, 2, 3, 4})) // prints: [1 2 3 4]
}

View file

@ -0,0 +1,35 @@
package main
import (
"fmt"
"math"
)
func uniq(list []float64) []float64 {
unique_set := map[float64]int{}
i := 0
nan := false
for _, x := range list {
if _, exists := unique_set[x]; exists {
continue
}
if math.IsNaN(x) {
if nan {
continue
} else {
nan = true
}
}
unique_set[x] = i
i++
}
result := make([]float64, len(unique_set))
for x, i := range unique_set {
result[i] = x
}
return result
}
func main() {
fmt.Println(uniq([]float64{1, 2, math.NaN(), 2, math.NaN(), 4})) // Prints [1 2 NaN 4]
}

View file

@ -0,0 +1,90 @@
package main
import (
"fmt"
"math"
"reflect"
)
func uniq(x interface{}) (interface{}, bool) {
v := reflect.ValueOf(x)
if !v.IsValid() {
panic("uniq: invalid argument")
}
if k := v.Kind(); k != reflect.Array && k != reflect.Slice {
panic("uniq: argument must be an array or a slice")
}
elemType := v.Type().Elem()
intType := reflect.TypeOf(int(0))
mapType := reflect.MapOf(elemType, intType)
m := reflect.MakeMap(mapType)
i := 0
for j := 0; j < v.Len(); j++ {
x := v.Index(j)
if m.MapIndex(x).IsValid() {
continue
}
m.SetMapIndex(x, reflect.ValueOf(i))
if m.MapIndex(x).IsValid() {
i++
}
}
sliceType := reflect.SliceOf(elemType)
result := reflect.MakeSlice(sliceType, i, i)
hadNaN := false
for _, key := range m.MapKeys() {
ival := m.MapIndex(key)
if !ival.IsValid() {
hadNaN = true
} else {
result.Index(int(ival.Int())).Set(key)
}
}
return result.Interface(), hadNaN
}
type MyType struct {
name string
value float32
}
func main() {
intArray := [...]int{5, 1, 2, 3, 2, 3, 4}
intSlice := []int{5, 1, 2, 3, 2, 3, 4}
stringSlice := []string{"five", "one", "two", "three", "two", "three", "four"}
floats := []float64{1, 2, 2, 4,
math.NaN(), 2, math.NaN(),
math.Inf(1), math.Inf(1), math.Inf(-1), math.Inf(-1)}
complexes := []complex128{1, 1i, 1 + 1i, 1 + 1i,
complex(math.NaN(), 1), complex(1, math.NaN()),
complex(math.Inf(+1), 1), complex(1, math.Inf(1)),
complex(math.Inf(-1), 1), complex(1, math.Inf(1)),
}
structs := []MyType{
{"foo", 42},
{"foo", 2},
{"foo", 42},
{"bar", 42},
{"bar", 2},
{"fail", float32(math.NaN())},
}
fmt.Print("intArray: ", intArray, " → ")
fmt.Println(uniq(intArray))
fmt.Print("intSlice: ", intSlice, " → ")
fmt.Println(uniq(intSlice))
fmt.Print("stringSlice: ", stringSlice, " → ")
fmt.Println(uniq(stringSlice))
fmt.Print("floats: ", floats, " → ")
fmt.Println(uniq(floats))
fmt.Print("complexes: ", complexes, "\n → ")
fmt.Println(uniq(complexes))
fmt.Print("structs: ", structs, " → ")
fmt.Println(uniq(structs))
// Passing a non slice or array will compile put
// then produce a run time panic:
//a := 42
//uniq(a)
//uniq(nil)
}

View file

@ -0,0 +1,22 @@
def list = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
assert list.size() == 12
println " Original List: ${list}"
// Filtering the List (non-mutating)
def list2 = list.unique(false)
assert list2.size() == 8
assert list.size() == 12
println " Filtered List: ${list2}"
// Filtering the List (in place)
list.unique()
assert list.size() == 8
println " Original List, filtered: ${list}"
def list3 = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
assert list3.size() == 12
// Converting to Set
def set = list as Set
assert set.size() == 8
println " Set: ${set}"

View file

@ -0,0 +1,3 @@
print $ unique [4, 5, 4, 2, 3, 3, 4]
[4,5,2,3]

View file

@ -0,0 +1,4 @@
import qualified Data.Set as Set
unique :: Ord a => [a] -> [a]
unique = Set.toList . Set.fromList

View file

@ -0,0 +1,8 @@
import Data.Set
unique :: Ord a => [a] -> [a]
unique = loop empty
where
loop s [] = []
loop s (x : xs) | member x s = loop s xs
| otherwise = x : loop (insert x s) xs

View file

@ -0,0 +1,5 @@
import Data.List
unique :: Eq a => [a] -> [a]
unique [] = []
unique (x : xs) = x : unique (filter (x /=) xs)

View file

@ -0,0 +1,3 @@
import Data.List
Data.List.nub :: Eq a => [a] -> [a]
Data.List.Unique.unique :: Ord a => [a] -> [a]

View file

@ -0,0 +1,9 @@
REAL :: nums(12)
CHARACTER :: workspace*100
nums = (1, 3, 2, 9, 1, 2, 3, 8, 8, 1, 0, 2)
WRITE(Text=workspace) nums ! convert to string
EDIT(Text=workspace, SortDelDbls=workspace) ! do the job for a string
READ(Text=workspace, ItemS=individuals) nums ! convert to numeric
WRITE(ClipBoard) individuals, "individuals: ", nums ! 6 individuals: 0 1 2 3 8 9 0 0 0 0 0 0

View file

@ -0,0 +1 @@
non_repeated_values = array[uniq(array, sort( array))]

View file

@ -0,0 +1,27 @@
100 PROGRAM "RemoveDu.bas"
110 RANDOMIZE
120 NUMERIC ARR(1 TO 20),TOP
130 LET TOP=FILL(ARR)
140 CALL WRITE(ARR,TOP)
150 LET TOP=REMOVE(ARR)
160 CALL WRITE(ARR,TOP)
170 DEF WRITE(REF A,N)
180 FOR I=1 TO N
190 PRINT A(I);
200 NEXT
210 PRINT
220 END DEF
230 DEF FILL(REF A)
240 LET FILL=UBOUND(A):LET A(LBOUND(A))=1
250 FOR I=LBOUND(A)+1 TO UBOUND(A)
260 LET A(I)=A(I-1)+RND(3)
270 NEXT
280 END DEF
290 DEF REMOVE(REF A)
300 LET ST=0
310 FOR I=LBOUND(A)+1 TO UBOUND(A)
320 IF A(I-1)=A(I) THEN LET ST=ST+1
330 IF ST>0 THEN LET A(I-ST)=A(I)
340 NEXT
350 LET REMOVE=UBOUND(A)-ST
360 END DEF

View file

@ -0,0 +1,15 @@
procedure main(args)
every write(!noDups(args))
end
procedure noDups(L)
every put(newL := [], notDup(set(),!L))
return newL
end
procedure notDup(cache, a)
if not member(cache, a) then {
insert(cache, a)
return a
}
end

View file

@ -0,0 +1,5 @@
To decide which list of Ks is (L - list of values of kind K) without duplicates:
let result be a list of Ks;
repeat with X running through L:
add X to result, if absent;
decide on result.

View file

@ -0,0 +1,4 @@
~. 4 3 2 8 0 1 9 5 1 7 6 3 9 9 4 2 1 5 3 2
4 3 2 8 0 1 9 5 7 6
~. 'chthonic eleemosynary paronomasiac'
chtoni elmsyarp

View file

@ -0,0 +1,10 @@
0 1 1 2 0 */0 1 2
0 0 0
0 1 2
0 1 2
0 2 4
0 0 0
~. 0 1 1 2 0 */0 1 2
0 0 0
0 1 2
0 2 4

View file

@ -0,0 +1,26 @@
fn deduplicated<T>(anon array: [T]) throws -> [T] {
mut existing_items: {T} = {}
mut result: [T] = []
for value in array {
if not existing_items.contains(value) {
existing_items.add(value)
result.push(value)
}
}
return result
}
fn deduplicated_set<T>(anon array: [T]) throws -> {T} {
mut result: {T} = {}
for value in array {
result.add(value)
}
return result
}
fn main() {
let array = [1, 2, 3, 3, 2, 5, 4]
println("{}", deduplicated(array))
println("{}", deduplicated_set(array))
}

View file

@ -0,0 +1,5 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

Some files were not shown because too many files have changed in this diff Show more