# Finding the most 'unique' word

Using you language of choice, write the shortest function/script/program you can that will identify the word with the highest number of unique letters in a text.

• Unique letters should include any distinct character using UTF-8 encoding.
• Upper and lower case versions of the same character are different and distinct; 'a' != 'A'
• Words are bound by any whitespace character.
• 'Letters' are any symbol which can be represented by a single unicode character.
• The output should be the word, followed by the count of unique letters.
• llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch - 18
• Any delimiter/spacing between the two values is up to you, so long as there is at least one character to separate them.
• In the event more than one word exists with the highest count, print all words for that count, with one new line delimiting.
superacknowledgement - 16
pseudolamellibranchiate - 16
• This is code golf, so shortest code wins.

This answer on English.SE inspired me to create this challenge. The example uses just a word list, but any text should be able to be processed.

• How are words separated? You say unique letters are any UTF-8 character, but that would imply that the entire file is just one word. – cardboard_box Feb 5 '13 at 14:54
• How are you defining letters here? As I've just been and pointed out on one of the English.SE answers LlanfairPG is a Welsh word and contains letters from the Welsh alphabet - ll and ch are both single letters in the Welsh language. – Gareth Feb 5 '13 at 16:12
• @Gareth I was not aware of that distinction, my mistake. Are there unicode representations of those two 'letters'? For the purposes of this challenge, each individual unicode character is a letter. – Gaffi Feb 5 '13 at 16:20
"for@{$_[-1]}  Interpretting the restriction "The text document must be read in by your code" to mean that command line options that read and parse the input are not allowed. As with the PHP solution below, only characters 10 and 32 are considered to be word delimiters. Input and output are also taken in the same manner. ## PHP 128 bytes <?foreach(split(~ßƒõ,fread(STDIN,1e6))as$s){$w[count(count_chars($s,1))][]=$s;}krsort($w)?><?=join($f=~ß.key($w).~õ,pos($w)),$f;


The only characters considered to be word delimiters are characer 10, and character 32. The rest, including puncuation, are considered to be part of the word.

This contains a few binary characters, which saves quotation marks, but as a result needs to be saved with an ANSI encoding in order to function properly. Alternatively, this version can be used, which is 3 bytes heavier:

<?foreach(split(' |
',fread(STDIN,1e6))as$s){$w[count(count_chars($s,1))][]=$s;}krsort($w)?><?=join($f=' '.key($w).' ',pos($w)),$f;  Sample I/O: input 1: It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way - in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only.  output 1: $ php most-unique.php < input1.dat
incredulity, 11


input 2:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec mollis, nisl sit
amet consequat fringilla, justo risus iaculis justo, vel ullamcorper dui tellus
ut enim. Suspendisse lectus risus, molestie sed volutpat nec, eleifend vitae
ligula. Nulla porttitor elit vel augue pretium cursus. Donec in turpis lectus.
Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia
Curae; Quisque a lorem eu turpis viverra sodales. Pellentesque justo arcu,
venenatis nec hendrerit a, molestie vitae augue.


output 2:

$php most-unique.php < input2.dat consequat 9 ullamcorper 9 Vestibulum 9  • incredulity has 10 unique letters, not 11. – DavidC Feb 5 '13 at 23:01 • @DavidCarraher his code is including the comma, which is theoretically allowed via the rules. – Shmiddty Feb 6 '13 at 0:37 • The explanation is absolutely incredulous. – DavidC Feb 6 '13 at 2:04 • Not only is it 'theoretically allowed', but given the wording of the question (specifically points 2 and 3), it seems to be a requirement. – primo Feb 6 '13 at 4:02 • @DavidCarraher Yes, punctuation are valid characters. Anything other than whitespace is valid. – Gaffi Feb 6 '13 at 12:38 ## GoRuby 2.0.0 – 66 chars The solutions below didn't actually find all matches but only one. Here's my final version: a=$<.r.sp.m{|x|[x,x.ch.u.sz]};a.m{|x|s x*' - 'if x.l==a.m_(&:l).l}


Examples:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec mollis, nisl sit amet consequat fringilla, justo risus iaculis justo, vel ullamcorper dui tellus ut enim. Suspendisse lectus risus, molestie sed volutpat nec, eleifend vitae ligula. Nulla porttitor elit vel augue pretium cursus. Donec in turpis lectus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Quisque a lorem eu turpis viverra sodales. Pellentesque justo arcu, venenatis nec hendrerit a, molestie vitae augue.

produces:

$ruby golf.rb < input.txt consequat - 9 ullamcorper - 9 Vestibulum - 9  ## GoRuby 2.0.0 – 29 chars (not exact output format) s$<.sp.m{|x|[x.ch.u.sz,x]}.mx


Expects input from stdin. The output format is a little bit different, though. For example:

$ruby golf.rb < british.1 14 manoeuvrability  ## GoRuby 2.0.0 – 42 40 chars s$<.r.sp.m{|x|[x.ch.u.sz,x]}.mx.rv*' - '


expects input from stdin

## Ruby 1.9.3 - 69 65 chars

puts\$<.read.split.map{|x|[x.chars.uniq.size,x]}.max.reverse*' - '


expects input from stdin (same as above, but without GoRuby abbreviations)

## Javascript 163 155 152 162 bytes

This is about as short as I can get it:

prompt(x=[]).split(/\s/).forEach(function(a){b={};c=0;a.split('').forEach(function(d){b[d]?1:b[d]=++c});x[c]?x[c].push(a):x[c]=[a]});alert((l=x.length-1)+':'+x[l])


prompt(x=[]).split(/\s/).map(function(a){b=[c=0];a.split('').map(function(d){b[d]?1:b[d]=++c});x[c]=(x[c]||[]).concat(a)});alert((l=x.length-1)+':'+x[l].join('\n'))


In this version /\s/ separates words based on whitespace, so it includes punctuation, commas, periods, etc as part of words. This is easily changed to /\b/ to not included them.

I'll see what I can do with for-loops instead of forEaches in a bit.

## I/O:

It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way - in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only.

11:incredulity,


Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec mollis, nisl sit amet consequat fringilla, justo risus iaculis justo, vel ullamcorper dui tellus ut enim. Suspendisse lectus risus, molestie sed volutpat nec, eleifend vitae ligula. Nulla porttitor elit vel augue pretium cursus. Donec in turpis lectus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Quisque a lorem eu turpis viverra sodales. Pellentesque justo arcu, venenatis nec hendrerit a, molestie vitae augue.

9:consequat
ullamcorper
Vestibulum


A little tired, perhaps. But I feel peaceful. Your success in the ring this morning was, to a small degree, my success. Your future is assured. You will live, secure and safe, Wilbur. Nothing can harm you now. These autumn days will shorten and grow cold. The leaves will shake loose from the trees and fall. Christmas will come, and the snows of winter. You will live to enjoy the beauty of the frozen world, for you mean a great deal to Zuckerman and he will not harm you, ever. Winter will pass, the days will lengthen, the ice will melt in the pasture pond. The song sparrow will return and sing, the frogs will awake, the warm wind will blow again. All these sights and sounds and smells will be yours to enjoy, Wilbur—this lovely world, these precious days…

10:Wilbur—this


Nearly all children nowadays were horrible. What was worst of all was that by means of such organizations as the Spies they were systematically turned into ungovernable little savages, and yet this produced in them no tendency whatever to rebel against the discipline of the Party. On the contrary, they adored the Party and everything connected with it... All their ferocity was turned outwards, against the enemies of the State, against foreigners, traitors, saboteurs, thought-criminals. It was almost normal for people over thirty to be frightened of their own children.

15:thought-criminals.

• There is a potential awkwardness with the output: if there are multiple words in the output and one of the words ends with a comma, it could show two commas in a row which would be confusing. – Shmiddty Feb 6 '13 at 16:43
• From the spec, In the event more than one word exists with the highest count, print all words for that count, **with one new line delimiting**. – Gaffi Feb 6 '13 at 19:38
• @Gaffi should be fixed now. 10 bytes >.< – Shmiddty Feb 6 '13 at 20:03

### Scala 129 chars:

def f{
println(l.filter(x=>x._2==l.last._2).mkString)}


R - 106 characters
As a function with the input text as parameter:

f=function(t){
s=strsplit
a=sapply
t=s(t," ")[[1]]
w=a(a(s(t,""),unique),length)
n=(w==max(w))
cbind(t[n],w[n])
}


And a few examples:

f("It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way - in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only.")
[,1]           [,2]
[1,] "incredulity," "11"

f("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec mollis, nisl sit amet consequat fringilla, justo risus iaculis justo, vel ullamcorper dui tellus ut enim. Suspendisse lectus risus, molestie sed volutpat nec, eleifend vitae ligula. Nulla porttitor elit vel augue pretium cursus. Donec in turpis lectus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Quisque a lorem eu turpis viverra sodales. Pellentesque justo arcu, venenatis nec hendrerit a, molestie vitae augue.")
[,1]          [,2]
[1,] "consequat"   "9"
[2,] "ullamcorper" "9"
[3,] "Vestibulum"  "9"


Or R - 100 characters
As a function with the path to the text file as parameter:

f=function(t){
t=scan(t,"")
a=sapply
w=a(a(strsplit(t,""),unique),length)
n=(w==max(w))
cbind(t[n],w[n])
}


Usage:

f("t1.txt")
[,1]           [,2]
[1,] "incredulity," "11"

• I think this is missing "The text document must be read in by your code". – Steven Rumbalski Feb 8 '13 at 15:47
• @StevenRumbalski this is corrected. – plannapus Feb 8 '13 at 16:12

Python 176 168

w = "".join((open('c')).readlines()).replace("\n", " ").split(" ")
l = sorted(zip([len(set(w[i])) for i in range(len(w))],w,))
print([x for x in l if l[-1][0] == x[0]])


# Python3 119

Reads from a file called a.

r={w:len(set(w))for w in open("a").read().split()};print("\n".join(str((k,v))for k,v in r.items()if v==max(r.values())))


Tested with the input texts from @primo:

Input 1:
('incredulity,', 11)

Input 2:
('Vestibulum', 9)
('consequat', 9)
('ullamcorper', 9)


## VBScript - 430 / VBA - 420

VBScript:

Function r(t)
d="Scripting.Dictionary"
Set w=CreateObject(d)
c=1
Do Until c>Len(t)
p=InStr(c,t," ")
i=InStr(c,t,vbCr)
If p<i Then s=i Else s=p
If s=0 Then s=Len(t)+1
f=Mid(t,c,s-c)
If Not w.Exists(f) Then
Set x=CreateObject(d)
For l=1 To Len(f)
n=Mid(f,l,1)
If Not x.Exists(n) Then x.Add n,n
Next
y=x.Count
If m=y Then z=f &vbCr &z
If m<y Then m=y:z=f
End If
c=s+1
Loop
r=z &" " &m
End Function


VBA:

Function r(t)
d="Scripting.Dictionary"
Set w=CreateObject(d)
c=1
Do Until c>Len(t)
p=InStr(c,t," ")
i=InStr(c,t,vbCr)
s=IIf(p<i,i,p)
If s=0 Then s=Len(t)+1
f=Mid(t,c,s-c)
If Not w.Exists(f) Then
Set x=CreateObject(d)
For l=1 To Len(f)
n=Mid(f,l,1)
If Not x.Exists(n) Then x.Add n,n
Next
`