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

61 Answers 61

1 2
3
0
\$\begingroup\$

Tcl, 93 bytes

proc c a\ b {expr [string le $a]<[string le $b]}
proc L l {return [lindex [lsort -c c $l] 0]}

Try it online!

Quite disappointed by the score, should/could be better. Thought about this one-proc version:

proc L l {
 set m  [lmap e $l {regsub -all . $e 1}]
 return [lindex $l [lsearch $m [expr max([join $m ,])]]]
}

but it's longer.

| improve this answer | |
\$\endgroup\$
1 2
3

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.