Unique characters in each string: Difference between revisions

m
(→‎{{header|JavaScript}}: Added a version in JavaScript)
m (→‎{{header|Wren}}: Minor tidy)
 
(30 intermediate revisions by 20 users not shown)
Line 14:
{{Template:Strings}}
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">V LIST = [‘1a3c52debeffd’, ‘2b6178c97a938stf’, ‘3ycxdb1fgxa2yz’]
 
print(sorted(Set(Array(LIST.join(‘’))).filter(ch -> all(:LIST.map(w -> w.count(@ch) == 1)))))</syntaxhighlight>
 
{{out}}
<pre>
[1, 2, 3, a, b, c]
</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">DEFINE MAX="128"
CHAR ARRAY uniq(MAX)
 
BYTE FUNC IsUnique(CHAR ARRAY s CHAR c)
BYTE count,i
 
count=0
FOR i=1 TO s(0)
DO
IF s(i)=c THEN
count==+1
IF count>1 THEN
RETURN (0)
FI
FI
OD
IF count=0 THEN
RETURN (0)
FI
RETURN (1)
 
PROC SetUnique(CHAR ARRAY s)
BYTE i,c
 
SetBlock(uniq,MAX,0)
FOR i=1 TO s(0)
DO
c=s(i)
IF IsUnique(s,c) THEN
uniq(c)=1
FI
OD
RETURN
 
PROC CheckUnique(CHAR ARRAY s)
BYTE i
 
FOR i=0 TO MAX-1
DO
IF uniq(i)=1 AND IsUnique(s,i)=0 THEN
uniq(i)=0
FI
OD
RETURN
 
PROC Main()
DEFINE PTR="CARD"
DEFINE CNT="3"
PTR ARRAY l(CNT)
INT i
 
l(0)="1a3c52debeffd"
l(1)="2b6178c97a938stf"
l(2)="3ycxdb1fgxa2yz"
 
SetUnique(l(0))
FOR i=1 TO CNT-1
DO
CheckUnique(l(i))
OD
FOR i=0 TO MAX-1
DO
IF uniq(i) THEN
Put(i) Put(32)
FI
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Unique_characters_in_each_string.png Screenshot from Atari 8-bit computer]
<pre>
1 2 3 a b c
</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
 
procedure Unique_Characters is
Line 47 ⟶ 133:
2 => Occurences ("2b6178c97a938stf"),
3 => Occurences ("3ycxdb1fgxa2yz")));
end Unique_Characters;</langsyntaxhighlight>
{{out}}
<pre>1 2 3 a b c</pre>
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">BEGIN # find the characters that occur once only in each element of a list #
# of stings #
OP UNIQUEINEACH = ( []STRING s )STRING:
IF INT required count = ( UPB s + 1 ) - LWB s;
required count < 1
THEN "" # the list is empty #
ELSE # non-empty list #
[ 0 : max abs char ]INT counts; # counts of used characters #
[ 0 : max abs char ]INT all;
FOR i FROM LWB all TO UPB all DO all[ i ] := 0 OD;
# count the occurances of the characters in the elements of s #
FOR i FROM LWB s TO UPB s DO
FOR i FROM LWB counts TO UPB counts DO counts[ i ] := 0 OD;
FOR j FROM LWB s[ i ] TO UPB s[ i ] DO
counts[ ABS s[ i ][ j ] ] +:= 1
OD;
FOR j FROM LWB counts TO UPB counts DO
# the character is unique in this string #
IF counts[ j ] = 1 THEN all[ j ] +:= 1 FI
OD
OD;
# construct a string of the characters that occur only once #
# in each string #
STRING result := "";
FOR i FROM LWB all TO UPB all DO
IF all[ i ] = required count THEN result +:= REPR i FI
OD;
result
FI; # UNIQUEINEACH #
# task test case #
STRING unique = UNIQUEINEACH []STRING( "1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz" );
FOR c FROM LWB unique TO UPB unique DO print( ( " ", unique[ c ] ) ) OD
END</syntaxhighlight>
{{out}}
<pre>
1 2 3 a b c
</pre>
 
=={{header|AppleScript}}==
Line 55 ⟶ 180:
The filtering here is case sensitive, the sorting dependent on locale.
 
<langsyntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
 
Line 75 ⟶ 200:
end uniqueCharactersInEachString
 
uniqueCharactersInEachString({"1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"})</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{"1", "2", "3", "a", "b", "c"}</langsyntaxhighlight>
 
===Core language only===
This can be case-insensitive if required. (Just leave out the 'considering case' statement round the call to the handler). The requirement for AppleScript 2.3.1 is only for the 'use' command which loads the "Heap Sort" script. If "Heap Sort"'s instead loaded with the older 'load script' command or copied into the code, this will work on systems as far back as Mac OS X 10.5 (Leopard) and possibly earlier. Same output as above.
 
<langsyntaxhighlight lang="applescript">use AppleScript version "2.3.1" -- OS X 10.9 (Mavericks) or later
use sorter : script "Heap Sort" -- <https://www.rosettacode.org/wiki/Sorting_algorithms/Heapsort#AppleScript>
 
Line 128 ⟶ 253:
considering case
uniqueCharactersInEachString({"1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"})
end considering</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">arr: ["1a3c52debeffd" "2b6178c97a938stf" "3ycxdb1fgxa2yz"]
uniques: split first arr
 
Line 141 ⟶ 266:
]
 
print sort uniques</langsyntaxhighlight>
 
{{out}}
Line 148 ⟶ 273:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f UNIQUE_CHARACTERS_IN_EACH_STRING.AWK
#
Line 180 ⟶ 305:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 188 ⟶ 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#}}==
<langsyntaxhighlight lang="fsharp">
// Unique characters in each string: Nigel Galloway. May 12th., 2021
let fN g=g|>Seq.countBy id|>Seq.filter(fun(_,n)->n=1)
let fUc g=g|>List.map fN|>Seq.concat|>Seq.countBy id|>Seq.filter(fun(_,n)->n=List.length g)|>Seq.map(fun((n,_),_)->n)|>Seq.sort
printfn "%s" (fUc ["1a3c52debeffd";"2b6178c97a938stf";"3ycxdb1fgxa2yz"]|>Array.ofSeq|>System.String)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 203 ⟶ 420:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: io kernel sequences.interleaved sets sorting ;
 
{ "1a3c52debeffd" "2b6178c97a938sf" "3ycxdb1fgxa2yz" }
Line 212 ⟶ 429:
! intersect-all obtain elements present in every string -> "1a3c2bf"
! [ duplicates ] gather obtain elements that repeat within a single string -> "efd798xy"
! without from the first string, remove elements that are in the second -> "1a3c2b"</langsyntaxhighlight>
{{out}}
<pre>
1 2 3 a b c
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">function count_char( s as string, c as string ) as uinteger
'count occurrences of character c in string s
dim as integer i, r = 0
for i = 1 to len(s)
if mid(s,i,1) = c then r += 1
next i
return r
end function
 
dim as string*20 dat(1 to 3) = {"1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"}
dim as string c, uniq
dim as integer i,j
 
for i = 1 to len(dat(1)) 'go through the first string
c = mid(dat(1),i,1)
for j = 1 to 3
if count_char(dat(j), c)<>1 then goto nexti 'contrary to popular belief, gotos are not evil
next j
for j = 1 to len(uniq)-1 'if it occurs once in every string
if mid(uniq,j+1,1)>c then exit for 'find where we need to put it in alphabetical order
next j
uniq = left(uniq,j)+c+right(uniq,len(uniq)-j) 'and insert it into its correct place
nexti:
next i
 
print uniq</syntaxhighlight>
{{out}}<pre>123abc</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn StringCharacterIsUnique( string as CFStringRef, chr as CFStringRef ) as BOOL
long count = 0, length = len(string)
CFRange range = fn StringRangeOfString( string, chr )
while ( range.location != NSNotFound )
count++
range.location++
range = fn StringRangeOfStringWithOptionsInRange( string, chr, 0, fn CFRangeMake(range.location,length-range.location) )
wend
end fn = (count == 1)
 
void local fn DoIt
CFArrayRef strings = @[@"1a3c52debeffd",@"2b6178c97a938stf",@"3ycxdb1fgxa2yz"]
CFStringRef chr
CFMutableArrayRef array = fn MutableArrayWithCapacity(0)
long i, j, count, length = len(strings[0])
for i = 0 to length - 1
chr = mid(strings[0],i,1)
if ( fn StringCharacterIsUnique( strings[0], chr ) )
count = 1
for j = 1 to len(strings) - 1
if ( fn StringCharacterIsUnique( strings[j], chr ) )
count++
end if
next
if ( count == len(strings) ) then MutableArrayAddObject( array, chr )
end if
next
MutableArraySortUsingSelector( array, @"compare:" )
print fn ArrayComponentsJoinedByString( array, @" " )
end fn
 
fn DoIt
 
HandleEvents
</syntaxhighlight>
 
{{out}}
<pre>
Line 219 ⟶ 510:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 248 ⟶ 539:
sort.Slice(chars, func(i, j int) bool { return chars[i] < chars[j] })
fmt.Println(string(chars))
}</langsyntaxhighlight>
 
{{out}}
Line 256 ⟶ 547:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import qualified Data.Map.Strict as M
import Data.Maybe (fromJust)
import qualified Data.Set as S
Line 286 ⟶ 577:
"2b6178c97a938stf",
"3ycxdb1fgxa2yz"
]</langsyntaxhighlight>
{{Out}}
<pre>123abc</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j"> /:~@> (e. # [)&.>/ (-. -.@~: # ])&.> '1a3c52debeffd';'2b6178c97a938stf';'3ycxdb1fgxa2yz'
123abc</syntaxhighlight>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
// --- CHARACTERS SEEN EXACTLY ONCE IN EACH STRING ---
 
// onceInEach :: [String] -> String
const onceInEach = ws =>
// Characters which occur exactly once
// in each word in thea given list.
0 < ws.length ? (() => {
const
wordCount = ws.length,
charFreqs = charCounts(ws.join("")),
sharedcharSet = ws.slices => new Set(1)[.reduce(..s]),
(a, x)wordCount => intersect(a)(new Set([..ws.x])),length;
new Set([...ws[0]])
);
 
return sort([
[...shared]ws.filterslice(1).reduceRight(
c(a, x) => wordCount === charFreqs[c]intersect(x)(
charSet(a)
),
charSet(ws[0])
)
).join("");]
.filter(c => wordCount === charFreqs[c])
.slice()
.sort()
.join("");
})() : "";
 
Line 322 ⟶ 623:
"3ycxdb1fgxa2yz"
]);
 
 
// --------------------- GENERIC ---------------------
Line 343 ⟶ 645:
b => new Set([...a].filter(i => b.has(i)));
 
 
// sort :: Ord a => [a] -> [a]
const sort = xs =>
// An A-Z sorted copy of xs.
[...xs].slice().sort(
(a, b) => a < b ? -1 : (a > b ? 1 : 0)
);
 
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>123abc</pre>
Line 362 ⟶ 657:
 
'''Helper functions'''
<langsyntaxhighlight lang="jq"># bag of words
def bow(stream):
reduce stream as $word ({}; .[($word|tostring)] += 1);
Line 386 ⟶ 681:
elif (.[0]|length) == 0 then empty
else [.[0], [ .[1:] | intersections]] | ios
end;</langsyntaxhighlight>
 
'''The task'''
<langsyntaxhighlight lang="jq">def once_in_each_string:
# convert each string to an array of the constituent characters
map((explode | map([.]|implode)))
Line 398 ⟶ 693:
["1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"]
| [once_in_each_string]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 405 ⟶ 700:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">list = ["1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"]
 
onceineachstring(list) = filter(c -> all(w -> count(x -> x == c, w) == 1, list), (sort ∘ unique ∘ prod)(list))
 
println(onceineachstring(list))
</langsyntaxhighlight>{{out}}<pre>
['1', '2', '3', 'a', 'b', 'c']
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">sets = Characters /@ {"1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"};
uniques = Sort[Select[Tally[#], Last/*EqualTo[1]][[All, 1]]] & /@ sets;
Intersection @@ uniques</syntaxhighlight>
{{out}}
<pre>{"1", "2", "3", "a", "b", "c"}</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strutils, tables
 
var result = AllChars
Line 425 ⟶ 727:
result = result * uniqueChars # Intersection.
 
echo result</langsyntaxhighlight>
 
{{out}}
<pre>{'1', '2', '3', 'a', 'b', 'c'}</pre>
 
=={{header|Pascal}}==
{{works with|Extended Pascal}}
<syntaxhighlight lang="pascal">program uniqueCharactersInEachString(output);
 
type
{ This “discriminates” the Extended Pascal (ISO 10206) schema }
{ data type `string` to hold a sequence of up to `16` characters. }
message = string(16);
characters = set of char;
 
{
\brief determine set of `char` values appear in a `string` once only
\param sample the `string` to inspect
\return a `set of char` appearing once in \param sample
}
{ In Extended Pascal `protected` denotes an immutable parameter. }
function uniqueCharacters(protected sample: message): characters;
var
{ Here, `sample.capacity` refers to `16`. }
i: 1..sample.capacity;
{ EP extension: `… value []` initializes variables to empty set value. }
firstOccurence, nonFirstOccurence: characters value [];
begin
for i := 1 to length(sample) do
begin
{ With sets, `+` denotes the union, `*` denotes the intersection. }
nonFirstOccurence := nonFirstOccurence + firstOccurence * [sample[i]];
firstOccurence := firstOccurence + [sample[i]]
end;
uniqueCharacters := firstOccurence - nonFirstOccurence
end;
 
{
\brief calls `uniqueCharacters` for several strings
\param sample the `array` of strings
\return characters appearing once in every \param sample member
}
function allUniqueCharacters(
{ This is a “conformant array parameter” as defined by }
{ ISO standard 7185 (level 1), and ISO standard 10206. }
protected sample: array[sampleMinimum..sampleMaximum: integer] of message
): characters;
var
{ `type of` is an Extended Pascal extension. }
i: type of sampleMinimum;
uniqueInEverySample: characters value [chr(0)..maxChar];
begin
for i := sampleMinimum to sampleMaximum do
begin
uniqueInEverySample := uniqueInEverySample * uniqueCharacters(sample[i])
end;
allUniqueCharacters := uniqueInEverySample
end;
 
 
{ === MAIN ============================================================= }
var
c: char;
sample: array[1..3] of message;
lonelyLetters: characters;
begin
sample[1] := '1a3c52debeffd';
sample[2] := '2b6178c97a938stf';
sample[3] := '3ycxdb1fgxa2yz';
lonelyLetters := allUniqueCharacters(sample);
{ If the compiler defines ascending evaluation order, in Extended Pascal }
{ it is also possible to write `for c in allUniqueCharacters(sample)`. }
{ However, the evaluation order (ascending/descending) is not defined by }
{ the ISO standard, but the compiler vendor. And note, while it’s }
{ guaranteed that `'A' < 'B'`, it is not guaranteed that `'a' < 'B'`. }
{ The following might (and actually on modern systems will) still produce }
{ a non-alphabetical listing. }
for c := chr(0) to maxChar do
begin
if c in lonelyLetters then
begin
{ The `:2` is a format specifier defining the width of the output. }
write(c:2)
end
end;
writeLn
end.</syntaxhighlight>
{{out}}
1 2 3 a b c
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Unique_characters_in_each_string
Line 441 ⟶ 832:
$chars !~ /$_.*$_/ && # the 'only once in each string' test
@strings == $chars =~ s/$_//g, # the 'in every string' test
$chars =~ /./g ]}\n";</langsyntaxhighlight>
{{out}}
<pre>
Line 448 ⟶ 839:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #000000;">sets</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 460 ⟶ 851:
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">intersection</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">),</span><span style="color: #000000;">once</span><span style="color: #0000FF;">}))</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"found %d unique common characters: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
found 6 unique common characters: 123abc
</pre>
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">import ordset.
 
main =>
L = ["1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"],
U = [new_ordset([C : C = 1 in counts(S)]) : S in L],
println(fold(intersection,U.first,U.tail)).
 
% Return a map of occurrences of each element in the list L
counts(L) = Map =>
Map = new_map(),
foreach(E in L)
Map.put(E,Map.get(E,0)+1)
end.</syntaxhighlight>
 
{{out}}
<pre>123abc</pre>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de acc (V K N)
(if (assoc K (val V))
(inc (nth (cadr @) N))
Line 487 ⟶ 896:
"1a3c52debeffd"
"2b6178c97a938stf"
"3ycxdb1fgxa2yz" ) ) )</langsyntaxhighlight>
{{out}}
<pre>
Line 494 ⟶ 903:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">LIST = ["1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"]
 
print(sorted([ch for ch in set([c for c in ''.join(LIST)]) if all(w.count(ch) == 1 for w in LIST)]))
</langsyntaxhighlight>{{out}}
<pre>
['1', '2', '3', 'a', 'b', 'c']
</pre>
 
 
Or, avoiding intermediate lists.
 
<syntaxhighlight lang="python">LIST = ["1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"]
print(sorted(ch for ch in set("".join(LIST)) if all(w.count(ch) == 1 for w in LIST)))</syntaxhighlight>
 
{{out}}
<pre>
['1', '2', '3', 'a', 'b', 'c']
</pre>
 
 
We can also avoid concatenating all strings in the input list.
 
<syntaxhighlight lang="python">from itertools import chain
 
LIST = ["1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"]
 
print(
sorted(
ch
for ch in set(chain.from_iterable(LIST))
if all(w.count(ch) == 1 for w in LIST)
)
)</syntaxhighlight>
 
{{out}}
<pre>
['1', '2', '3', 'a', 'b', 'c']
</pre>
 
 
Or, avoid calling <code>count()</code> for every distinct character in every string in the input list.
 
<syntaxhighlight lang="python">from collections import Counter
 
LIST = ["1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"]
 
print(
sorted(
set.intersection(
*(
set(char for char, count in counts.items() if count == 1)
for counts in (Counter(s) for s in LIST)
)
)
)
)</syntaxhighlight>
 
{{out}}
<pre>
['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}}==
 
<syntaxhighlight lang="raku" perl6line>my $strings = <1a3c52debeffd 2b6178c97a938stf 3ycxdb1fgxa2yz>;
 
put sort keys [∩] $strings.map: *.comb.Bag.grep: *.value == 1</langsyntaxhighlight>
{{out}}
<pre>1 2 3 a b c</pre>
Line 520 ⟶ 1,013:
 
On an &nbsp;'''EBCDIC'''&nbsp; machine, &nbsp; the lowercase letters and the uppercase letters &nbsp; aren't &nbsp; contiguous.
<langsyntaxhighlight lang="rexx">/*REXX pgm finds and shows characters that are unique in each string and once only. */
parse arg $ /*obtain optional arguments from the CL*/
if $='' | $="," then $= '1a3c52debeffd' "2b6178c97a938stf" '3ycxdb1fgxa2yz'
Line 543 ⟶ 1,036:
say 'unique characters are: ' @ /*display the unique characters found. */
say
say 'Found ' L " unique characters." /*display the # of unique chars found. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 552 ⟶ 1,045:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
see "working..." + nl
see "Unique characters in each string are:" + nl
Line 598 ⟶ 1,091:
end
return sum
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 606 ⟶ 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>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">arr = ["1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"]
 
uniqs_in_str = arr.map{|str| str.chars.tally.filter_map{|char, count| char if count == 1} }
puts uniqs_in_str.inject(&:intersection).sort.join(" ")
</syntaxhighlight>
</lang>
{{out}}
<pre>1 2 3 a b c</pre>
 
=={{header|Transd}}==
<syntaxhighlight lang="Scheme">#lang transd
 
MainModule: {
_start: (lambda locals: pos Position<String>() b Bool()
(for c in (sort "1a3c52debeffd") do (= b true)
(for str in ["1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"] do
(= pos (find str c))
(if (or (is-end pos) (find Range(in: str (+ (get-idx pos) 1) -0) c))
(= b false)))
(if b (textout c " "))
) )
}</syntaxhighlight>
{{out}}
<pre>
1 2 3 a b c
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-seq}}
{{libheader|Wren-sort}}
<langsyntaxhighlight ecmascriptlang="wren">import "./seq" for Lst
import "./sort" for Sort
 
var strings = ["1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"]
Line 633 ⟶ 1,168:
Sort.insertion(uniqueChars)
System.print("Found %(uniqueChars.count) unique character(s) common to each string, namely:")
System.print(uniqueChars.join(" "))</langsyntaxhighlight>
 
{{out}}
Line 640 ⟶ 1,175:
1 2 3 a b c
</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">fn main() {
strings := ["1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"]!
mut u := map[rune]int{}
for s in strings {
mut m := map[rune]int{}
for c in s {
m[c]++
}
for k, v in m {
if v == 1 {
u[k]++
}
}
}
mut chars := []rune{}
for k, v in u {
if v == 3 {
chars << k
}
}
chars.sort()
println(chars.string())
}</syntaxhighlight>
 
{{out}}
<pre>
123abc
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">char List, Counter(128), Unique(3, 128);
int Char, I, J;
string 0; \use null-terminated string convention
[List:= ["1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"];
for I:= 0 to 2 do \each string in List
[for J:= 0 to 127 do Counter(J):= 0;
J:= 0; \count each character in string
repeat Char:= List(I, J);
J:= J+1;
Counter(Char):= Counter(Char)+1;
until Char = 0; \terminator
for J:= 0 to 127 do
Unique(I, J):= Counter(J) = 1;
];
for Char:= 1 to 127 do \skip 0 terminator
if Unique(0, Char) & Unique(1, Char) & Unique(2, Char) then
[ChOut(0, Char); ChOut(0, ^ )];
]</syntaxhighlight>
 
{{out}}
<pre>
1 2 3 a b c
</pre>
 
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">sub count_char(s$, c$)
local i, r
r = 0
for i = 1 to len(s$)
if mid$(s$,i,1) = c$ then r = r + 1 : fi
next i
return r
end sub
dim dat$(2)
dat$(0) = "1a3c52debeffd"
dat$(1) = "2b6178c97a938stf"
dat$(2) = "3ycxdb1fgxa2yz"
for i = 1 to len(dat$(1))
c$ = mid$(dat$(1), i, 1)
for j = 0 to 2
if count_char(dat$(j), c$) <> 1 then goto nexti : fi
next j
for j = 0 to len(uniq$)-1
if mid$(uniq$, j+1, 1) > c$ then break : fi
next j
uniq$ = left$(uniq$, j) + c$ + right$(uniq$, len(uniq$)-j)
label nexti
next i
print uniq$
end</syntaxhighlight>
{{out}}
<pre>123abc</pre>
9,476

edits