29
\$\begingroup\$

Your challenge is to take an array of strings and output the longest string in the array. So for

["tiny", "small", "bigger", "biggest"]

the output would be biggest.

If two elements of the array have the same length, you should choose the one that appears first in the array. That means if the array looks like this:

["one", "two", "no"]

the output is one, but if the array looked like this:

["two", "one", "no"]

the output is two.


As this is , the shortest code in bytes wins.

\$\endgroup\$
8
  • 4
    \$\begingroup\$ 2 notes: 1 It's heavily discouraged if the question is changed and invalidates existing answer, and 2 the Sandbox exists for exactly that reason (make sure challenges are good before posting) \$\endgroup\$
    – user202729
    Commented Dec 20, 2017 at 10:54
  • 4
    \$\begingroup\$ Since I don't think anyone else has mentioned it -- Hi, and welcome to PPCG! \$\endgroup\$ Commented Dec 20, 2017 at 14:21
  • 1
    \$\begingroup\$ No you don't have to handle the case where the array is empty. But if you want you can. \$\endgroup\$
    – Doggo
    Commented Dec 20, 2017 at 14:25
  • 5
    \$\begingroup\$ 2 hours? That's far, far too quick to be accepting an answer. \$\endgroup\$
    – Shaggy
    Commented Dec 20, 2017 at 15:05
  • 7
    \$\begingroup\$ Normally you wait a week \$\endgroup\$
    – user63187
    Commented Dec 20, 2017 at 16:43

71 Answers 71

1 2
3
0
\$\begingroup\$

05AB1E, 3 bytes

Réθ

Try it online!

Explanation

R     # reverse
 é    # sort by length
  θ   # get the last element
\$\endgroup\$
0
\$\begingroup\$

PHP, 59 bytes

while($n=strlen($s=$argv[++$i]))$n>$e&&$e=$n+!$r=$s;echo$r;

takes words as separate command line arguments. Run with -nr or try it online.

\$\endgroup\$
0
\$\begingroup\$

Google Sheets, 40 Bytes

Anonymous worksheet function that takes input from range A:A and outputs to the calling cell

=Index(A:A,Match(Max(Len(A:A)),Len(A:A),
\$\endgroup\$
0
\$\begingroup\$

PHP, 69 bytes

usort($x,function($a,$b){return strlen($b)<=>strlen($a);});echo$x[0];

Not a trick or nice golfing, just sorting array by string length and then outputting the first element.

\$\endgroup\$
0
\$\begingroup\$

APL NARS, 10 chars, 24 bytes

{⍵[↑⍒⍴¨⍵]}

note in the test, that: 'uno' it is not one array of array, so the array of correct deep is ,⊂'uno' instead

⎕fmt  ,⊂'uno'    
  ┌1─────┐
  │┌3───┐│
  ││ uno││
  │└────┘2
  └∊─────┘

so in the test g fail for 'uno'

g←{⍵[↑⍒⍴¨⍵]}
g 'uno'
  u

but not fail for the correct type of input

g ,⊂'uno'
  uno
g 'uno' 'quattro' 'cinque'
  quattro
⎕fmt  g ,⊂''
  ┌───┐
  │┌0┐│
  ││ ││
  │└¯┘2
  └∊──┘

if the community think this is one too much big limitation: than there is the 18 chars solution

{1=≡⍵:⍵⋄∊⍵[↑⍒⍴¨⍵]} 

that evade the problem in the first test calculate the deep of input

f←{1=≡⍵:⍵⋄∊⍵[↑⍒⍴¨⍵]}     
f 'uno'
  uno
f 'uno' 'quattro' 'cinque'
  quattro

The problem of the input type of one Char in Apl as 'a' (it is consider from interpreter one char). We can transform that in one array of array of char, or one type list of arrays of deep 2 write ,⊂,'a'; so g ,⊂,'a' should return the correct answer of type array of array length 1

\$\endgroup\$
4
  • \$\begingroup\$ Please try not to make spelling and grammar error... \$\endgroup\$
    – user202729
    Commented Jan 8, 2018 at 11:01
  • \$\begingroup\$ @user202729 for the spelling error this computer seems sufficient, for grammar error nothing \$\endgroup\$
    – user58988
    Commented Jan 8, 2018 at 11:23
  • \$\begingroup\$ My computer clearly shows the warning. \$\endgroup\$
    – user202729
    Commented Jan 8, 2018 at 11:35
  • \$\begingroup\$ @user202729 ok, I use one different phrase (for eccessive); thank you very much, and a good year \$\endgroup\$
    – user58988
    Commented Jan 8, 2018 at 11:51
0
\$\begingroup\$

V, 27 23 21 20 bytes

Í./_
ÚGYu/"
uYHVGp

Try it online!

Explanation:

Í./_         In every line, replace every character by _
Ú            Sort alphabetically. Cursor ends up in first line
 GY          Go to last line, copy it
   u         Undo sort
    /"       Search for string in register " (the one that was just copied). Non-printable character is <C-r>
u            Undo replace, go to last cursor position. Non-printable character is <C-o> (thanks, Cows quack)
  Y          Copy line
   HVG       Go to first line, select all lines until last line
      p      Paste copied line into selection

Hexdump:

00000000: cd2e 2f5f 0ada 4759 752f 1222 0a75 0f59  ../_..GYu/.".u.Y
00000010: 4856 4770                                HVGp
\$\endgroup\$
1
0
\$\begingroup\$

C++ (gcc), 65 63 bytes

Accepts any container of std::string and returns via reference.

[](auto&c,auto&r){r=c[0];for(auto x:c)r=x.size()>r.size()?x:r;}

Try it online!

If you are tolerant to output, there is a 58 byte solution which alters the input list and copies the longest string to the first element:

[](auto&c){for(auto x:c)c[0]=x.size()>c[0].size()?x:c[0];}
\$\endgroup\$
0
\$\begingroup\$

sed 4.2.2 -rz, 71 72 83 bytes

h
:
s/^.//gm
/[^\n]{2}/t
:b
/^\n/{s/.//
x
s/^[^\n]*.//
x}
tb
x
s/\n.*//

Try it online!

  • -r uses extended regular expressions, in other words, allows golifer syntax as we are able to save several \s

  • -z puts all lines of input into one "line", so that they would be processed at the same time rather than the normal cyclic processing.

This was a fun challenge seeing as sed did not have any builtin sort functions. The program works as follows:

  • copy the pattern space into the hold space
  • find the position of the longest line by repeatedly removing the first character from every line until we are remaining with line(s) with only a single character, this first of these is at the position of the longest line
  • to find which line this position corresponds to, we repeatedly

    • remove the first line of the pattern space and hold space until we encounter a non-newline character on the first line of the pattern space
  • now that the corresponding line is at the top of the hold space, we remove the other lines
  • the contents of the buffer is printed at the end of the program
\$\endgroup\$
0
\$\begingroup\$

Factor, 7 bytes

longest

Try it online!

Builtin

\$\endgroup\$
0
\$\begingroup\$

Noulith, 33 bytes

\l->l find\w->w.len==max(l map len)

Try it online!

Explanation

\l->                                # lambda, taking in a list of strings
    l find                          # find in `l`...
          \w->                      # some word `w` such that...
              w.len                 # the length of `w`...
                   ==               # is equal to...
                     max(l map len) # the longest `w` in `l`
\$\endgroup\$
0
\$\begingroup\$

Raku (Perl 6) (rakudo), 17 bytes

{@_.max: *.chars}

Attempt This Online!

{@_.     # split list
max:     # find the max of each
*.chars} # based on num of chars
\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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