Unique characters in each string: Difference between revisions

m
(FutureBasic solution added)
m (→‎{{header|Wren}}: Minor tidy)
 
(7 intermediate revisions by 5 users not shown)
Line 313:
3 strings, 43 characters, 20 different, 6 unique: 123abc
</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">(∧∘(∊/⊣)´ (∊∧∊⌾⌽)⊸/¨) "1a3c52debeffd"‿"2b6178c97a938stf"‿"3ycxdb1fgxa2yz"</syntaxhighlight>
{{out}}
<pre>"123abc"</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
var SA: array [0..2] of string = ('1a3c52debeffd', '2b6178c97a938stf', '3ycxdb1fgxa2yz');
 
 
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;
 
 
function CommonToAllStrs(SA: array of string): string;
{Get all the letters shared by all the strings in SA}
var I,J,Cnt: integer;
var S1: string;
var C: char;
begin
Result:='';
{Exit if empty array}
if Length(SA)<1 then exit;
{Get the first string}
S1:=SA[0];
for I:=1 to Length(S1) do
begin
{Character from 1st string }
C:=S1[I];
{Count # of occurences}
Cnt:=1;
for J:=1 to High(SA) do
if Pos(C,SA[J])>0 then Inc(Cnt);
{grab it if it appears in all other string}
if Cnt=Length(SA) then Result:=Result+C;
end;
end;
 
procedure ShowCharsAppearOnce(Memo: TMemo);
var I: integer;
var S: string;
var SS: array of string;
begin
SetLength(SS,0);
{Get all single appearance characters}
for I:=0 to High(SA) do
begin
SetLength(SS,Length(SS)+1);
SS[High(SS)]:=CharsAppearingOnce(SA[I]);
end;
{Get the ones shared by all string}
S:=CommonToAllStrs(SS);
Memo.Lines.Add(S);
end;
</syntaxhighlight>
{{out}}
<pre>
123abc
 
Elapsed Time: 1.238 ms.
 
</pre>
 
 
=={{header|F_Sharp|F#}}==
Line 372 ⟶ 464:
{{out}}<pre>123abc</pre>
 
=={{header|GoFutureBasic}}==
<syntaxhighlight lang="gofuturebasic">package main
 
import (
"fmt"
"sort"
)
 
func main() {
strings := []string{"1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"}
u := make(map[rune]int)
for _, s := range strings {
m := make(map[rune]int)
for _, c := range s {
m[c]++
}
for k, v := range m {
if v == 1 {
u[k]++
}
}
}
var chars []rune
for k, v := range u {
if v == 3 {
chars = append(chars, k)
}
}
sort.Slice(chars, func(i, j int) bool { return chars[i] < chars[j] })
fmt.Println(string(chars))
}</syntaxhighlight>
 
{{out}}
<pre>
123abc
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">
local fn StringCharacterIsUnique( string as CFStringRef, chr as CFStringRef ) as BOOL
long count = 0, length = len(string)
Line 452 ⟶ 507:
<pre>
1 2 3 a b c
</pre>
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"sort"
)
 
func main() {
strings := []string{"1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"}
u := make(map[rune]int)
for _, s := range strings {
m := make(map[rune]int)
for _, c := range s {
m[c]++
}
for k, v := range m {
if v == 1 {
u[k]++
}
}
}
var chars []rune
for k, v := range u {
if v == 3 {
chars = append(chars, k)
}
}
sort.Slice(chars, func(i, j int) bool { return chars[i] < chars[j] })
fmt.Println(string(chars))
}</syntaxhighlight>
 
{{out}}
<pre>
123abc
</pre>
 
Line 488 ⟶ 580:
{{Out}}
<pre>123abc</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j"> /:~@> (e. # [)&.>/ (-. -.@~: # ])&.> '1a3c52debeffd';'2b6178c97a938stf';'3ycxdb1fgxa2yz'
123abc</syntaxhighlight>
 
=={{header|JavaScript}}==
Line 868 ⟶ 964:
['1', '2', '3', 'a', 'b', 'c']
</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ 0 128 of
swap witheach
[ 2dup peek
1+ unrot poke ] ] is countchars ( $ --> [ )
 
[ [] swap witheach
[ 1 = join ] ] is justones ( [ --> [ )
 
[ witheach
[ over i^ peek +
swap i^ poke ] ] is addnests ( [ [ --> [ )
 
[ [] swap witheach
[ 3 = if [ i^ join ] ] ] is threesfound ( [ --> $ )
 
$ "1a3c52debeffd" nested
$ "2b6178c97a938stf" nested join
$ "3ycxdb1fgxa2yz" nested join
 
witheach [ countchars justones ]
2 times addnests
threesfound
witheach [ emit sp ]</syntaxhighlight>
 
{{out}}
 
<pre>1 2 3 a b c </pre>
 
=={{header|Raku}}==
Line 973 ⟶ 1,099:
Found 6 unique characters in each string
done...
</pre>
 
=={{header|RPL}}==
{{works with|HP|48G}}
≪ → word char
≪ 0
1 word SIZE '''FOR''' j
word j DUP SUB char == +
'''NEXT'''
≫ ≫ '<span style="color:blue">OCCHAR</span>' STO
≪ "" → words char
≪ { } words 1 GET
1 OVER SIZE '''FOR''' j
DUP j DUP SUB 'char' STO
words 1 ≪ char <span style="color:blue">OCCHAR</span> ≫ DOLIST
'''IF''' ΠLIST 1 == '''THEN''' SWAP char + SWAP '''END'''
'''NEXT''' DROP SORT
≫ ≫ '<span style="color:blue">UNICHARS</span>' STO
 
{ "1a3c52debeffd" "2b6178c97a938stf" "3ycxdb1fgxa2yz" } <span style="color:blue">UNICHARS</span>
{{out}}
<pre>
1: { "1" "2" "3" "a" "b" "c" }
</pre>
 
Line 1,005 ⟶ 1,155:
{{libheader|Wren-seq}}
{{libheader|Wren-sort}}
<syntaxhighlight lang="ecmascriptwren">import "./seq" for Lst
import "./sort" for Sort
 
var strings = ["1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"]
9,482

edits