Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 30s
Walk input with Hashtbl.mem + Hashtbl.add seen x () (unit-payload
turns the table into a set); on first occurrence cons to the result
list; reverse at the end:
let stable_unique xs =
let seen = Hashtbl.create 8 in
let result = ref [] in
List.iter (fun x ->
if not (Hashtbl.mem seen x) then begin
Hashtbl.add seen x ();
result := x :: !result
end
) xs;
List.rev !result
For [3;1;4;1;5;9;2;6;5;3;5;8;9]:
result = [3;1;4;5;9;2;6;8] (input order, dupes dropped)
length = 8, sum = 38 total = 46
Tests Hashtbl as a set abstraction (unit-payload), the rev-build
idiom, and 'not (Hashtbl.mem seen x)' membership negation.
82 baseline programs total.
16 lines
367 B
OCaml
16 lines
367 B
OCaml
let stable_unique xs =
|
|
let seen = Hashtbl.create 8 in
|
|
let result = ref [] in
|
|
List.iter (fun x ->
|
|
if not (Hashtbl.mem seen x) then begin
|
|
Hashtbl.add seen x ();
|
|
result := x :: !result
|
|
end
|
|
) xs;
|
|
List.rev !result
|
|
|
|
;;
|
|
|
|
List.length (stable_unique [3;1;4;1;5;9;2;6;5;3;5;8;9])
|
|
+ List.fold_left (+) 0 (stable_unique [3;1;4;1;5;9;2;6;5;3;5;8;9])
|