A person has two first names if their last name is also a common first name. You are tasked with determining which full names in a list are two first names.

John Smith
John Doe
Luke Ryan
Ryan Johnson
Jenna Jackson
Tom John

Any name that occurs in the first name column is potentially a first name. If the number of occurrences of the name in the first name column is greater than the number of occurrences in the last name column, it is definitely a first name.

In the above list, John appears twice in the first names and once in the last names so it is definitely a first name. Ryan appears once in the first and once in the last so it is (probably) a first name.

Therefore, Tom John definitely has two first names and Luke Ryan probably does.

Given the above list, your code should output the follow:

Luke Ryan has two first names
Tom John definitely has two first names


As mentioned above, your code will take in a list of full names (from standard input, one per line) separated by spaces. Names can include hyphens or apostrophes, but you will never be given a first or last name that includes spaces (ie no Liam De Rosa, but Liam De-Rosa or Liam De'Rosa are fair game. In other words, names will match [-'A-Za-z]+.

Each full name will be unique (ie John Smith will not appear twice).


Print full names names (once per line) followed by either has two first names or definitely has two first names if they meet the criteria above. Names should only be printed once.

Names that are not two first names do not need to be printed.

You must preserve the case and special characters of the name.



Madison Harris
Riley Hudson
Addison Hills
Riley Phillips
Scott Hill
Levi Murphy
Hudson Wright
Nathan Baker
Harper Brooks
Chloe Morris
Aubrey Miller
Hudson Lopez
Samuel Owen
Wyatt Victoria
Brooklyn Cox
Nathan Murphy
Ryan Scott


Riley Hudson definitely has two first names
Ryan Scott has two first names


Owen Parker
Daniel Hall
Cameron Hall
Sofia Watson
Mia Murphy
Ryan Jones
Emily Ramirez


[no output]


Olivia Robinson
Jacob van-Dyke
Jacob Ella
Brayden De'Rosa
Levi Brook
Brook Bella
Ella Hill
Ella Anderson
Brook-Anne van-Dyke


Jacob Ella definitely has two first names
Levi Brook has two first names

Notes and Scoring

Good luck!

  • \$\begingroup\$ Does Johnson count as John, or is it different? \$\endgroup\$ – NoOneIsHere Oct 19 '17 at 15:40
  • 2
    \$\begingroup\$ Johnson would be different than John. Names must match exactly. \$\endgroup\$ – Moose Oct 19 '17 at 15:43
  • 1
    \$\begingroup\$ How we choose between definitely has two first names and has two first names ? It can be always one of those? \$\endgroup\$ – Rod Oct 19 '17 at 15:46
  • \$\begingroup\$ Only display definitely if the the name occurs more times in the first-name column than the last-name column. This case overrides, so only display definitely has two first names. Check out the examples. \$\endgroup\$ – Moose Oct 19 '17 at 15:49
  • 2
    \$\begingroup\$ How flexible is input format? Can it be an array of strings, one per person? Or a 2D array of strings? \$\endgroup\$ – Luis Mendo Oct 19 '17 at 16:08

14 Answers 14


Java (OpenJDK 8), 238 222 221 220 212 bytes

l->{for(String n:l){int k=0,j=0,q=0;for(String b=n.split(" ")[1];k<l.length;j+=l[k++].matches(b+" .*")?1:0)q+=l[k].endsWith(" "+b)?1:0;if(j>0)System.out.println(n+(j>q?" definitely":"")+" has two first names");}}

Try it online!


Python 2, 141 137 132 130 bytes

-2 bytes thanks to @JonathanFrech

for f,l in n:
 c=cmp(*[x.count(l)for x in zip(*n)])
 if~c:print f,l,'definitely '*c+'has two first names'

Try it online!

  • \$\begingroup\$ I think -c<1 can be ~c, as c does not appear to ever get positive. \$\endgroup\$ – Jonathan Frech Oct 20 '17 at 21:05

AWK, 127 123 bytes

N[X]=$0}END{for(;++i<=X;)if(A=a[L[i]])print N[i],(b[L[i]]<A?"definitely ":"")"has two first names"}

Saving 1 byte by not using the built-in NR value.

Try it online!


Perl 5, 120 118 + 2 (-al) = 120 bytes

$f{$F[0]}++;$l{$k{$_}=$F[1]}++}{map{print$_.' definitely'x($f{$t}>$l{$t})." has two first names"if$f{$t=$k{$_}}}keys%k

Try it online!


Ruby, 105 129 bytes

+24 bytes because I missed part of the spec

->a{a.map{|e|l=e[r=/\S+$/];i=a.count{|n|n[/\S+/]==l};e+"#{' definitely'if i>a.count{|n|n[r]==l}} has two first names"if i>0}-[p]}

Try it online!


Python 2, 140 127 122 131 bytes

for f,l in N:
 if l in F:print f,l,'definitely '*(F.count(l)>L.count(l))+'has two first names'

Try it online!

  • \$\begingroup\$ @ShreevatsaR, Ah, I misread. Thanks :) \$\endgroup\$ – TFeld Oct 20 '17 at 8:17

05AB1E, 144 bytes (Work in progress)

|vy#Dθˆн})©gF®®NèQO¯®NèQO-D®¯NèQO¯¯NèQO-D.À>0›s>0›&i0›s0›&i®Nè" "¯Nè" defínítely has two fírst names"J,ë®Nè" "¯Nè" has two fírst names"J,}ë\\}}´
  • 5
    \$\begingroup\$ No it works, I am just working on reducing the number of bytes \$\endgroup\$ – David Smith Oct 20 '17 at 0:09

05AB1E, 53 51 bytes

|UXv“€°‚•€ÛŒî“D„´Î ì)yð«ìõ¸ìXð¡øεy#θQO}`.S>sèˆ}¯õK»

Try it online!


|                                                       # Take inputs as array
 UX                                                     # Store in X and push X to stack
   v                                          }         # For each name, do
    “€°‚•€ÛŒî“D                                         #   Push "has two first names" twice
               „´Î ì                                    #   Prepend "definitely " to one of those
                    )yð«ì                               #   Wrap both strings in an array and prepend (name + " ") to each
                         õ¸ì                            #   Prepend " " to array
                            Xð¡øεy#θQO}`                #   Get occurences in input first and last names
                                        .S>             #   0 for not first name, 1 for first name and 2 for definitely first name
                                           sèˆ          #   Get string at that index and push to global array
                                               ¯õK»     # Output global array, 1 string per line

Or 51 49 bytes, assuming standard I/O Rules (input and output as arrays)

UXv“€°‚•€ÛŒî“D„´Î ì)yð«ìõ¸ìXð¡øεy#θQO}`.S>sèˆ}¯õK

Try it online!


PHP, 172 bytes

for(;$s=$argv[++$i];$f[$a]++,$l[$n[]=$b]++)[$a,$b]=explode(" ",$s);for(;$b=$n[+$k++];)$f[$b]<$l[$b]||print$argv[$k].($f[$b]>$l[$b]?" definetly":"")." has two first names

takes names as separate command line arguments.

Run with -nr or try it online.


Python 3, 187 bytes

while 1:
 except:[print(a,b,'definitely has two first names'[(n[b]>0)*11:])for a,b in c if n[b]>=0];break

R, 207 bytes

s='definitively has two first names\n'
x=matrix(t(unlist(sapply(n,strsplit,' '))),2)
for(i in n){a=apply(x[2,j]==x,1,sum)

Try it online!


JavaScript (ES6), 149 bytes

s=>s.replace(/(.*) (.*)\n/g,(_,t,u)=>g(`
${u} `)>1?t+` ${u}${v>g(` ${u}
`)?` definitely`:``} has two first names

I/O includes trailing newline. Largely inspired by @RobertoGraham's answer.


Haskell, 144 140 139 bytes

g.map words.lines
g s=id=<<[n++' ':f++[c|c<-" definitely",t 0>t 1]++" has two first names\n"|[n,f]<-s,let t i=filter(==f)$map(!!i)s,[]<t 0]

Try it online!


PowerShell, 176 bytes

$s=" has two first names"
$l|%{switch(($f-match$_).count){{$_-eq1}{$n[$i]+$s}{$_-gt1}{$n[$i]+" definitely"+$s}}$i++}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.