Unique characters: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 15:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">DefaultDict[Char, Int] d
L(s) [‘133252abcdeeffd’, ‘a6789798st’, ‘yxcdfgxcyz’]
L(c) s
Line 22:
L(k) sorted(d.keys())
I d[k] == 1
print(k, end' ‘’)</langsyntaxhighlight>
 
{{out}}
Line 30:
 
=={{header|8080 Assembly}}==
<langsyntaxhighlight lang="8080asm">puts: equ 9 ; CP/M print syscall
TERM: equ '$' ; CP/M string terminator
org 100h
Line 86:
;;; Memory
upage: equ ($/256)+1 ; Workspace for 'unique'
outbuf: equ (upage+1)*256 ; Output </langsyntaxhighlight>
{{out}}
<pre>156bgstz</pre>
 
=={{header|8086 Assembly}}==
<langsyntaxhighlight lang="asm"> cpu 8086
org 100h
puts: equ 9 ; MS-DOS syscall to print a string
Line 141:
section .bss
uniqws: resb 256
outbuf: resb 256</langsyntaxhighlight>
{{out}}
<pre>156bgstz</pre>
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">DEFINE MAX="128"
CHAR ARRAY counts(MAX)
 
Line 192:
FI
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Unique_characters.png Screenshot from Atari 8-bit computer]
Line 200:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
 
procedure Unique_Characters is
Line 230:
Count ("yxcdfgxcyz");
Put_Only_Once;
end Unique_Characters;</langsyntaxhighlight>
{{out}}
<pre>1 5 6 b g s t z</pre>
Line 236:
=={{header|ALGOL 68}}==
Case sensitive. This assumes a small character set (e.g. ASCII where max abs char is 255). Would probably need some work if CHAR is Unicode.
<langsyntaxhighlight lang="algol68">BEGIN # find the characters that occur once only in a list of stings #
# returns the characters that occur only once in the elements of s #
OP UNIQUE = ( []STRING s )STRING:
Line 257:
# task test case #
print( ( UNIQUE []STRING( "133252abcdeeffd", "a6789798st", "yxcdfgxcyz" ), newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 267:
The filtering here is case sensitive, the sorting dependent on locale.
 
<langsyntaxhighlight lang="applescript">on uniqueCharacters(listOfStrings)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
Line 279:
return (mutableSet's sortedArrayUsingDescriptors:({sortDescriptor})) as list
end uniqueCharacters</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{"1", "5", "6", "b", "g", "s", "t", "z"}</langsyntaxhighlight>
 
===Core language only===
This isn't quite as fast as the ASObjC solution above, but it can be case-insensitive if required. (Simply leave out the 'considering case' statement round the call to the handler). The requirement for AppleScript 2.3.1 is just for the 'use' command which loads the "Heap Sort" script. If "Heap Sort"'s loaded differently or compiled directly into the code, this script will work on systems at least 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 321:
considering case
return uniqueCharacters({"133252abcdeeffd", "a6789798st", "yxcdfgxcyz"})
end considering</langsyntaxhighlight>
 
 
Line 328:
Composing a solution from existing generic primitives, for speed of drafting and refactoring, and for high levels of code reuse.
 
<langsyntaxhighlight lang="applescript">use framework "Foundation"
 
 
Line 481:
((current application's NSArray's arrayWithArray:xs)'s ¬
sortedArrayUsingSelector:"compare:") as list
end sort</langsyntaxhighlight>
{{Out}}
<pre>{"1", "5", "6", "b", "g", "s", "t", "z"}</pre>
 
=={{header|APL}}==
<langsyntaxhighlight APLlang="apl">uniques ← (⊂∘⍋⌷⊣)∘(∪(/⍨)(1=(≢⊢))⌸)∘∊</langsyntaxhighlight>
{{out}}
<pre> uniques '133252abcdeeffd' 'a6789798st' 'yxcdfgxcyz'
Line 493:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">arr: ["133252abcdeeffd" "a6789798st" "yxcdfgxcyz"]
str: join arr
 
print sort select split str 'ch -> 1 = size match str ch</langsyntaxhighlight>
 
{{out}}
Line 503:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f UNIQUE_CHARACTERS.AWK
#
Line 529:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 539:
 
=={{header|BASIC}}==
<langsyntaxhighlight lang="basic">10 DEFINT A-Z
20 DIM C(255)
30 READ A$: IF A$="" GOTO 90
Line 554:
140 DATA "a6789798st"
150 DATA "yxcdfgxcyz"
160 DATA ""</langsyntaxhighlight>
{{out}}
<pre>156bgstz</pre>
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let uniques(strings, out) be
Line 587:
uniques(strings, out)
writef("%S*N", out)
$)</langsyntaxhighlight>
{{out}}
<pre>156bgstz</pre>
 
=={{header|BQN}}==
<langsyntaxhighlight lang="bqn">Uniq ← (⍷/˜1=/⁼∘⊐)∧∘∾</langsyntaxhighlight>
 
{{out}}
Line 601:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
 
Line 633:
printf("%s\n", uniques(strings, buf));
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>156bgstz</pre>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <map>
 
Line 653:
}
std::cout << '\n';
}</langsyntaxhighlight>
 
{{out}}
Line 662:
=={{header|Factor}}==
{{works with|Factor|0.99 build 2074}}
<langsyntaxhighlight lang="factor">USING: io sequences sets.extras sorting ;
 
{ "133252abcdeeffd" "a6789798st" "yxcdfgxcyz" }
concat non-repeating natural-sort print</langsyntaxhighlight>
{{out}}
<pre>
Line 673:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">Dim As Integer c(255), i, a
Dim As String s
Do
Line 689:
 
Data "133252abcdeeffd", "a6789798st", "yxcdfgxcyz", ""
Sleep</langsyntaxhighlight>
{{out}}
<pre>156bgstz</pre>
Line 695:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 718:
sort.Slice(chars, func(i, j int) bool { return chars[i] < chars[j] })
fmt.Println(string(chars))
}</langsyntaxhighlight>
 
{{out}}
Line 726:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (group, sort)
 
uniques :: [String] -> String
Line 739:
"a6789798st",
"yxcdfgxcyz"
]</langsyntaxhighlight>
{{Out}}
<pre>156bgstz</pre>
Line 745:
 
Or folding the strings down to a hash of character frequencies:
<langsyntaxhighlight lang="haskell">import qualified Data.Map.Strict as M
 
--------- UNIQUE CHARACTERS FROM A LIST OF STRINGS -------
Line 768:
"a6789798st",
"yxcdfgxcyz"
]</langsyntaxhighlight>
{{Out}}
<pre>156bgstz</pre>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 823:
// MAIN ---
return JSON.stringify(main());
})();</langsyntaxhighlight>
{{Out}}
<pre>["1","5","6","b","g","s","t","z"]</pre>
Line 830:
Or, folding the strings (with Array.reduce) down to a hash of character frequencies:
 
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 859:
 
return JSON.stringify(main());
})();</langsyntaxhighlight>
{{Out}}
<pre>["1","5","6","b","s","t","g","z"]</pre>
Line 865:
=={{header|J}}==
The simple approach here is to merge the argument strings and find characters which occur exactly once in that intermediate result:
<langsyntaxhighlight Jlang="j">uniques=: ~.#~1=#/.~</langsyntaxhighlight>
In other words, <code>~.</code> finds the distinct characters, <code>#/.~</code> finds the corresponding counts of those characters, so <code>1=#/.~</code> is true for the characters which occur exactly once, and <code>#~</code> filters the distinct characters based on those truth values.
{{out}}
Line 877:
'''Works with gojq, the Go implementation of jq'''
 
The following "bag-of-words" solution is quite efficient as it takes advantage of the fact that jq implements JSON objects as a hash.<langsyntaxhighlight lang="jq">
# bag of words
def bow(stream):
Line 886:
def in_one_just_once:
bow( .[] | explode[] | [.] | implode) | with_entries(select(.value==1)) | keys;
</syntaxhighlight>
</lang>
'''The task'''
<langsyntaxhighlight lang="jq">["133252abcdeeffd", "a6789798st", "yxcdfgxcyz"]
| in_one_just_once</langsyntaxhighlight>
{{out}}
<pre>
Line 896:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">list = ["133252abcdeeffd", "a6789798st", "yxcdfgxcyz"]
 
function is_once_per_all_strings_in(a::Vector{String})
Line 905:
 
println(is_once_per_all_strings_in(list))
</langsyntaxhighlight>{{out}}<pre>
['1', '5', '6', 'b', 'g', 's', 't', 'z']
</pre>
One might think that the method above suffers from too many passes through the text with one pass per count, but with a small text length the dictionary lookup takes more time. Compare times for a single pass version:
 
<langsyntaxhighlight lang="julia">function uniquein(a)
counts = Dict{Char, Int}()
for c in prod(list)
Line 923:
@btime is_once_per_all_strings_in(list)
@btime uniquein(list)
</langsyntaxhighlight>{{out}}<pre>
['1', '5', '6', 'b', 'g', 's', 't', 'z']
1.740 μs (28 allocations: 3.08 KiB)
Line 930:
 
This can be rectified (see Phix entry) if we don't save the counts as we go but just exclude entries with duplicates:
<langsyntaxhighlight lang="julia">function uniquein2(a)
s = sort(collect(prod(list)))
l = length(s)
Line 939:
 
@btime uniquein2(list)
</langsyntaxhighlight>{{out}}<pre>
['1', '5', '6', 'b', 'g', 's', 't', 'z']
1.010 μs (14 allocations: 1.05 KiB)
Line 946:
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">local strings = {"133252abcdeeffd", "a6789798st", "yxcdfgxcyz"}
unpack = unpack or table.unpack -- compatibility for all Lua versions
 
Line 970:
end
table.sort (list)
print (unpack (list))</langsyntaxhighlight>
{{out}}<pre>1 5 6 b g s t z</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Select[Tally[Sort[Characters[StringJoin[{"133252abcdeeffd", "a6789798st", "yxcdfgxcyz"}]]]], Last /* EqualTo[1]][[All, 1]]</langsyntaxhighlight>
{{out}}
<pre>{"1", "5", "6", "b", "g", "s", "t", "z"}</pre>
Line 981:
One solution, but others are possible, for instance concatenating the strings and building the count table from it rather than merging several count tables. And to build the last sequence, we could have used something like <code>sorted(toSeq(charCount.pairs).filterIt(it[1] == 1).mapIt(it[0]))</code>, which is a one liner but less readable and less efficient than our solution using “collect”.
 
<langsyntaxhighlight Nimlang="nim">import algorithm, sugar, tables
 
var charCount: CountTable[char]
Line 992:
if count == 1: ch
 
echo sorted(uniqueChars)</langsyntaxhighlight>
 
{{out}}
Line 999:
=={{header|Pascal}}==
{{works with|Extended Pascal}}
<langsyntaxhighlight lang="pascal">program uniqueCharacters(output);
 
type
Line 1,055:
writeLn
end.</langsyntaxhighlight>
{{out}}
<pre>156bgstz</pre>
Line 1,061:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl"># 20210506 Perl programming solution
 
use strict;
Line 1,073:
"133252abcdeeffd", "a6789798st", "yxcdfgxcyz", "AАΑSäaoö٥🤔👨‍👩‍👧‍👧";
my $uca = Unicode::Collate->new();
print $uca->sort ( grep { $seen{$_} == 1 } keys %seen )</langsyntaxhighlight>
{{out}}
<pre>
Line 1,080:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">once</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
Line 1,090:
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</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 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 1,097:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de uni (Lst
(let R NIL
(mapc
Line 1,115:
"133252abcdeeffd"
"a6789798st"
"yxcdfgxcyz" ) ) )</langsyntaxhighlight>
{{out}}
<pre>
Line 1,122:
 
=={{header|PL/M}}==
<langsyntaxhighlight lang="pli">100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
Line 1,170:
CALL PRINT(.BUFFER);
CALL EXIT;
EOF</langsyntaxhighlight>
{{out}}
<pre>156BGSTZ</pre>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">'''Unique characters'''
 
from itertools import chain, groupby
Line 1,209:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>['1', '5', '6', 'b', 'g', 's', 't', 'z']</pre>
 
Or reducing the given strings down to a hash of character frequencies:
<langsyntaxhighlight lang="python">'''Unique characters'''
 
from functools import reduce
Line 1,255:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>['1', '5', '6', 'b', 'g', 's', 't', 'z']</pre>
Line 1,262:
One has to wonder where the digits 0 through 9 come in the alphabet... 🤔 For that matter, What alphabet should they be in order of? Most of these entries seem to presuppose ASCII order but that isn't specified anywhere. What to do with characters outside of ASCII (or Latin-1)? Unicode ordinal order? Or maybe DUCET Unicode collation order? It's all very vague.
 
<syntaxhighlight lang="raku" perl6line>my @list = <133252abcdeeffd a6789798st yxcdfgxcyz>;
 
for @list, (@list, 'AАΑSäaoö٥🤔👨‍👩‍👧‍👧') {
Line 1,269:
"\n (DUCET) Unicode collation order: ",
.map( *.comb ).Bag.grep( *.value == 1 )».key.collate.join, "\n";
}</langsyntaxhighlight>
{{out}}
<pre>133252abcdeeffd a6789798st yxcdfgxcyz
Line 1,289:
 
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 to only one string and once only.*/
parse arg $ /*obtain optional arguments from the CL*/
if $='' | $="," then $= '133252abcdeeffd' "a6789798st" 'yxcdfgxcyz' /*use defaults.*/
Line 1,307:
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 inputs:}}
<pre>
Line 1,316:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
see "working..." + nl
see "Unique characters are:" + nl
Line 1,349:
end
return sum
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,360:
 
=={{header|Vlang}}==
<langsyntaxhighlight lang="vlang">fn main() {
strings := ["133252abcdeeffd", "a6789798st", "yxcdfgxcyz"]
mut m := map[rune]int{}
Line 1,384:
})
println(chars.string())
}</langsyntaxhighlight>
 
{{out}}
Line 1,394:
{{libheader|Wren-seq}}
{{libheader|Wren-sort}}
<langsyntaxhighlight lang="ecmascript">import "/seq" for Lst
import "/sort" for Sort
 
Line 1,402:
Sort.insertion(uniqueChars)
System.print("Found %(uniqueChars.count) unique character(s), namely:")
System.print(uniqueChars.join(" "))</langsyntaxhighlight>
 
{{out}}
Line 1,413:
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="yabasic">dim c(255)
data "133252abcdeeffd", "a6789798st", "yxcdfgxcyz", ""
repeat
Line 1,427:
next i
print s$
end</langsyntaxhighlight>
{{out}}
<pre>
Line 1,435:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">int List, I, N, C;
char Tbl(128), Str;
string 0;
Line 1,451:
for I:= 0 to 127 do
if Tbl(I) = 1 then ChOut(0, I);
]</langsyntaxhighlight>
 
{{out}}
10,333

edits