Unique characters: Difference between revisions

Add two StandardML versions
(Add two StandardML versions)
 
(8 intermediate revisions by 7 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,391 ⟶ 1,472:
</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
≪ DUP SIZE → string length
≪ 1 length '''FOR''' n
string n DUP SUB
'''NEXT'''
length 1 '''FOR''' n
1 n 1 - START
'''IF''' DUP2 ≥ '''THEN''' SWAP '''END'''
n ROLLD
'''NEXT'''
n ROLLD
-1 '''STEP'''
2 length '''START''' + '''NEXT'''
≫ ≫
‘SORTS’ STO
≪ DUP 1 DUP SUB → str char1
≪ str SIZE
'''IF''' DUP 1 >
'''THEN'''
DROP 1
'''WHILE''' str OVER 1 + DUP SUB char1 == '''REPEAT''' 1 + '''END'''
'''END'''
char1
≫ ≫
‘OCHST’ STO
≪ "" 1 3 PICK SIZE '''FOR''' j
OVER j GET +
'''NEXT'''
SWAP DROP
SORTS "" SWAP 1
'''WHILE''' OVER SIZE OVER ≥ '''REPEAT'''
DUP2 OVER SIZE SUB OCHST
'''IF''' OVER 1 ==
'''THEN''' 5 ROLL SWAP + 4 ROLLD
'''ELSE''' DROP
'''END'''
+
'''END'''
DROP2
‘UNCHR’ STO
===Shorter code but increased memory requirements===
≪ → strings
≪ { 255 } 0 CON 1 strings SIZE '''FOR''' j
strings j GET 1 OVER SIZE '''FOR''' k
DUP k DUP SUB NUM ROT SWAP DUP2 GET 1 + PUT SWAP
'''NEXT'''
DROP
'''NEXT'''
"" 1 255 '''FOR''' j
'''IF''' OVER j GET 1 == '''THEN''' j CHR + '''END'''
'''NEXT'''
SWAP DROP
‘UNCHR’ STO
 
{"133252abcdeeffd", "a6789798st", "yxcdfgxcyz"} UNCHR
{{out}}
<pre>
1: "156bgstz"
</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,407 ⟶ 1,564:
</pre>
 
=={{header|VlangStandard ML}}==
Using an Array:
<syntaxhighlight lang="vlang">fn main() {
<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() {
strings := ["133252abcdeeffd", "a6789798st", "yxcdfgxcyz"]
mut m := map[rune]int{}
Line 1,418 ⟶ 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,442 ⟶ 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,457 ⟶ 1,658:
1 5 6 b g s t z
</pre>
 
 
=={{header|Yabasic}}==
23

edits