Jump to content

Unique characters in each string: Difference between revisions

m
syntax highlighting fixup automation
m (Python alternatives)
m (syntax highlighting fixup automation)
Line 18:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V LIST = [‘1a3c52debeffd’, ‘2b6178c97a938stf’, ‘3ycxdb1fgxa2yz’]
 
print(sorted(Set(Array(LIST.join(‘’))).filter(ch -> all(:LIST.map(w -> w.count(@ch) == 1)))))</langsyntaxhighlight>
 
{{out}}
Line 28:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">DEFINE MAX="128"
CHAR ARRAY uniq(MAX)
 
Line 94:
FI
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Unique_characters_in_each_string.png Screenshot from Atari 8-bit computer]
Line 102:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
 
procedure Unique_Characters is
Line 133:
2 => Occurences ("2b6178c97a938stf"),
3 => Occurences ("3ycxdb1fgxa2yz")));
end Unique_Characters;</langsyntaxhighlight>
{{out}}
<pre>1 2 3 a b c</pre>
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN # find the characters that occur once only in each element of a list #
# of stings #
OP UNIQUEINEACH = ( []STRING s )STRING:
Line 170:
STRING unique = UNIQUEINEACH []STRING( "1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz" );
FOR c FROM LWB unique TO UPB unique DO print( ( " ", unique[ c ] ) ) OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 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 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 253:
considering case
uniqueCharactersInEachString({"1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"})
end considering</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">arr: ["1a3c52debeffd" "2b6178c97a938stf" "3ycxdb1fgxa2yz"]
uniques: split first arr
 
Line 266:
]
 
print sort uniques</langsyntaxhighlight>
 
{{out}}
Line 273:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f UNIQUE_CHARACTERS_IN_EACH_STRING.AWK
#
Line 305:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 315:
 
=={{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 328:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: io kernel sequences.interleaved sets sorting ;
 
{ "1a3c52debeffd" "2b6178c97a938sf" "3ycxdb1fgxa2yz" }
Line 337:
! 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>
Line 344:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight 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
Line 369:
next i
 
print uniq</langsyntaxhighlight>
{{out}}<pre>123abc</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 402:
sort.Slice(chars, func(i, j int) bool { return chars[i] < chars[j] })
fmt.Println(string(chars))
}</langsyntaxhighlight>
 
{{out}}
Line 410:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import qualified Data.Map.Strict as M
import Data.Maybe (fromJust)
import qualified Data.Set as S
Line 440:
"2b6178c97a938stf",
"3ycxdb1fgxa2yz"
]</langsyntaxhighlight>
{{Out}}
<pre>123abc</pre>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 507:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>123abc</pre>
Line 516:
 
'''Helper functions'''
<langsyntaxhighlight lang="jq"># bag of words
def bow(stream):
reduce stream as $word ({}; .[($word|tostring)] += 1);
Line 540:
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 552:
["1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"]
| [once_in_each_string]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 559:
 
=={{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}}==
<langsyntaxhighlight Mathematicalang="mathematica">sets = Characters /@ {"1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"};
uniques = Sort[Select[Tally[#], Last/*EqualTo[1]][[All, 1]]] & /@ sets;
Intersection @@ uniques</langsyntaxhighlight>
{{out}}
<pre>{"1", "2", "3", "a", "b", "c"}</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strutils, tables
 
var result = AllChars
Line 586:
result = result * uniqueChars # Intersection.
 
echo result</langsyntaxhighlight>
 
{{out}}
Line 593:
=={{header|Pascal}}==
{{works with|Extended Pascal}}
<langsyntaxhighlight lang="pascal">program uniqueCharactersInEachString(output);
 
type
Line 676:
end;
writeLn
end.</langsyntaxhighlight>
{{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 691:
$chars !~ /$_.*$_/ && # the 'only once in each string' test
@strings == $chars =~ s/$_//g, # the 'in every string' test
$chars =~ /./g ]}\n";</langsyntaxhighlight>
{{out}}
<pre>
Line 698:
 
=={{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 710:
<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>
Line 717:
 
=={{header|Picat}}==
<langsyntaxhighlight Picatlang="picat">import ordset.
 
main =>
Line 729:
foreach(E in L)
Map.put(E,Map.get(E,0)+1)
end.</langsyntaxhighlight>
 
{{out}}
Line 735:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de acc (V K N)
(if (assoc K (val V))
(inc (nth (cadr @) N))
Line 755:
"1a3c52debeffd"
"2b6178c97a938stf"
"3ycxdb1fgxa2yz" ) ) )</langsyntaxhighlight>
{{out}}
<pre>
Line 762:
 
=={{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']
Line 773:
Or, avoiding intermediate lists.
 
<langsyntaxhighlight 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)))</langsyntaxhighlight>
 
{{out}}
Line 784:
We can also avoid concatenating all strings in the input list.
 
<langsyntaxhighlight lang="python">from itertools import chain
 
LIST = ["1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"]
Line 794:
if all(w.count(ch) == 1 for w in LIST)
)
)</langsyntaxhighlight>
 
{{out}}
Line 804:
Or, avoid calling <code>count()</code> for every distinct character in every string in the input list.
 
<langsyntaxhighlight lang="python">from collections import Counter
 
LIST = ["1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"]
Line 817:
)
)
)</langsyntaxhighlight>
 
{{out}}
Line 826:
=={{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 842:
 
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 865:
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 874:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
see "working..." + nl
see "Unique characters in each string are:" + nl
Line 920:
end
return sum
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 931:
 
=={{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>
Line 942:
{{libheader|Wren-seq}}
{{libheader|Wren-sort}}
<langsyntaxhighlight lang="ecmascript">import "/seq" for Lst
import "/sort" for Sort
 
Line 955:
Sort.insertion(uniqueChars)
System.print("Found %(uniqueChars.count) unique character(s) common to each string, namely:")
System.print(uniqueChars.join(" "))</langsyntaxhighlight>
 
{{out}}
Line 965:
=={{header|Vlang}}==
{{trans|go}}
<langsyntaxhighlight lang="vlang">fn main() {
strings := ["1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"]!
mut u := map[rune]int{}
Line 987:
chars.sort()
println(chars.string())
}</langsyntaxhighlight>
 
{{out}}
Line 995:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">char List, Counter(128), Unique(3, 128);
int Char, I, J;
string 0; \use null-terminated string convention
Line 1,012:
if Unique(0, Char) & Unique(1, Char) & Unique(2, Char) then
[ChOut(0, Char); ChOut(0, ^ )];
]</langsyntaxhighlight>
 
{{out}}
Line 1,021:
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="yabasic">sub count_char(s$, c$)
local i, r
r = 0
Line 1,048:
print uniq$
end</langsyntaxhighlight>
{{out}}
<pre>123abc</pre>
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.