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.

  • 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\$
    Dec 20, 2017 at 10:54
  • 4
    \$\begingroup\$ Since I don't think anyone else has mentioned it -- Hi, and welcome to PPCG! \$\endgroup\$ 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
    Dec 20, 2017 at 14:25
  • 5
    \$\begingroup\$ 2 hours? That's far, far too quick to be accepting an answer. \$\endgroup\$
    – Shaggy
    Dec 20, 2017 at 15:05
  • 7
    \$\begingroup\$ Normally you wait a week \$\endgroup\$
    – user63187
    Dec 20, 2017 at 16:43

66 Answers 66

1 2

V, 27 23 21 20 bytes


Try it online!


Í./_         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


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

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];}

sed 4.2.2 -rz, 71 72 83 bytes


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

Factor, 7 bytes


Try it online!



Uiua, 6 bytes


Try it!

     .  # duplicate
   ∵⧻   # lengths
 ⊢⍖     # index of maximum
⊡       # pick

Noulith, 33 bytes

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

Try it online!


\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`
1 2

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.