Unique characters: Difference between revisions

Add two StandardML versions
(Add two StandardML versions)
 
(5 intermediate revisions by 5 users not shown)
Line 659:
156bgstz
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
var SA: array [0..2] of string = ('133252abcdeeffd', 'a6789798st', 'yxcdfgxcyz');
 
function CharsAppearingOnce(S: string): string;
{Return all character that only occur once}
var SL: TStringList;
var I,Inx: integer;
begin
SL:=TStringList.Create;
try
{Store each character and store a count}
{of the number of occurances in the object}
for I:=1 to Length(S) do
begin
{Check to see if letter is already in list}
Inx:=SL.IndexOf(S[I]);
{Increment the count if it is, otherwise store it}
if Inx>=0 then SL.Objects[Inx]:=Pointer(Integer(SL.Objects[Inx])+1)
else SL.AddObject(S[I],Pointer(1));
end;
{Sort the list}
SL.Sort;
{Now return letters with a count of one}
Result:='';
for I:=0 to SL.Count-1 do
if integer(SL.Objects[I])<2 then Result:=Result+SL[I];
finally SL.Free; end;
end;
 
procedure ShowUniqueChars(Memo: TMemo);
var I: integer;
var S: string;
begin
{Concatonate all strings}
S:='';
for I:=0 to High(SA) do S:=S+SA[I];
{Get all characters that appear once}
S:=CharsAppearingOnce(S);
Memo.Lines.Add(S);
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
156bgstz
 
Elapsed Time: 0.959 ms.
 
</pre>
 
 
=={{header|Factor}}==
Line 1,290 ⟶ 1,348:
{{Out}}
<pre>['1', '5', '6', 'b', 'g', 's', 't', 'z']</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ [] swap
witheach join
[] 0 128 of
rot witheach
[ 2dup peek
1+ unrot poke ]
witheach
[ 1 = if
[ i^ join ] ] ] is task ( [ --> $ )
 
[]
$ "133252abcdeeffd" nested join
$ "a6789798st" nested join
$ "yxcdfgxcyz" nested join
 
task echo$</syntaxhighlight>
 
{{out}}
 
<pre>156bgstz</pre>
 
=={{header|Raku}}==
Line 1,457 ⟶ 1,538:
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">words = ["133252abcdeeffd", "a6789798st", "yxcdfgxcyz"]
 
counter = words.inject({}){|h, word| word.chars.tally(h)}
puts counter.filter_map{|char, count| char if count == 1}.sort.join
</syntaxhighlight>
{{out}}
<pre>
156bgstz
</pre>
=={{header|Transd}}==
<syntaxhighlight lang="Scheme">#lang transd
Line 1,472 ⟶ 1,563:
156bgstz
</pre>
 
=={{header|Standard ML}}==
Using an Array:
<syntaxhighlight lang="sml">
fun uniqueChars xs =
let
val arr = Array.array(256, 0)
val inc = (fn c => Array.update(arr, ord c, Array.sub(arr, ord c)+1))
val _ = List.app inc (List.concat (List.map String.explode xs))
val ex1 = (fn (i,n,a) => if n=1 then (chr i)::a else a)
in
String.implode (Array.foldri ex1 [] arr)
end
</syntaxhighlight>
{{out}}
<pre>
- uniqueChars ["133252abcdeeffd","a6789798st","yxcdfgxcyz"];
val it = "156bgstz" : string
</pre>
 
A different approach:
<syntaxhighlight lang="sml">
(*
group [1,1,2,4,4,4,2,2,2,1,1,1,3]
=> [[1,1], [2], [4,4,4], [2,2,2], [1,1,1], [3]]
*)
fun group xs =
let
fun collectGroups(a,[]) = [[a]]
| collectGroups(a,b::bs) = if a = (hd b) then (a::b)::bs else [a]::b::bs
in
List.foldr collectGroups [] xs
end
 
fun uniqueChars2 xs =
let
(* turn the strings into one big list of characters *)
val cs = List.concat (List.map String.explode xs)
(* sort the big list of characters *)
val scs = ListMergeSort.sort Char.> cs
(* collect the groups *)
val gs = group scs
(* filter out groups with more than one member *)
val os = List.filter (fn a => null (tl a)) gs
in
String.implode (List.concat os)
end
</syntaxhighlight>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn main() {
fn main() {
strings := ["133252abcdeeffd", "a6789798st", "yxcdfgxcyz"]
mut m := map[rune]int{}
Line 1,484 ⟶ 1,624:
mut chars := []rune{}
for k, v in m {
if v == 1 {chars << k}
chars << k
}
}
chars.sort_with_compare(fn(i &rune, j &rune) int {
if *i < *j {return -1}
if *i > *j {return -1}
return 0
})
println(chars.string())
}
</syntaxhighlight>
if *i>*j {
return 1
}
return 0
})
println(chars.string())
}</syntaxhighlight>
 
{{out}}
Line 1,508 ⟶ 1,643:
{{libheader|Wren-seq}}
{{libheader|Wren-sort}}
<syntaxhighlight lang="ecmascriptwren">import "./seq" for Lst
import "./sort" for Sort
 
var strings = ["133252abcdeeffd", "a6789798st","yxcdfgxcyz"]
Line 1,523 ⟶ 1,658:
1 5 6 b g s t z
</pre>
 
 
=={{header|Yabasic}}==
23

edits