Jump to content

Unique characters: Difference between revisions

→‎{{header|AppleScript}}: Added a solution using only the core language.
(→‎{{header|AppleScript}}: Added a solution using only the core language.)
Line 15:
 
=={{header|AppleScript}}==
===AppleScriptObjC===
The filtering here is case sensitive, the sorting dependent on locale.
 
<lang applescript>use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
 
Line 38 ⟶ 40:
{{output}}
<lang applescript>{"1", "5", "6", "b", "g", "s", "t", "z"}</lang>
 
===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.
 
<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>
 
on uniqueCharacters(listOfStrings)
script o
property allCharacters : {}
property uniques : {}
end script
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
set o's allCharacters to text items of (listOfStrings as text)
set AppleScript's text item delimiters to astid
set characterCount to (count o's allCharacters)
tell sorter to sort(o's allCharacters, 1, characterCount)
set i to 1
set currentCharacter to beginning of o's allCharacters
repeat with j from 2 to characterCount
set thisCharacter to item j of o's allCharacters
if (thisCharacter is not currentCharacter) then
if (j - i = 1) then set end of o's uniques to currentCharacter
set i to j
set currentCharacter to thisCharacter
end if
end repeat
if (i = j) then set end of o's uniques to currentCharacter
return o's uniques
end uniqueCharacters
 
considering case
return uniqueCharacters({"133252abcdeeffd", "a6789798st", "yxcdfgxcyz"})
end considering</lang>
 
=={{header|Factor}}==
557

edits

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