[challenge] Return Largest Numbers in Arrays

For a challenge solution, your code is absolutely fine and does everything it should do.

Regarding it being longer, do you mean lines of code or execution time? If it’s lines of code, you shouldn’t worry about trying to condense everything in as tight as possible. It’s much more important that your code is readable than packed in as tight as possible.

Regarding execution time, your code “theoretically” will take longer to execute than the solution you found. This is because you use the array sort() method. What you are doing in your solution is actually sorting every array, which takes longer than just looking for the largest item. If you think about it, a sort algorithm needs to find the biggest value and put it first. But then it needs to find the “second” biggest item and place it second, and again for the third, and so on and so on. The problem only wants the biggest from an array, so a lot of extra work is being done by sort() that doesn’t need to be. Practically, it’s probably not a big deal because you’ll only start to see issues when you start scaling the data (think an array with a billion elements). In computer science terms, the run-time complexity of your solution is most likely O(n log n) since it is based on sort(), while the run-time complexity of the solution you found is only O(n) (I am deliberately assuming that the number of arrays being sorted is insignificant.)

If this were production code, the one point I’d make about your solution is that it changes the arrays passed in to it, which may be ok, or may not be. The sort() method sorts an array in place, effectively changing it for the caller. This may be ok or not depending on who is using it. A comment mentioning this would be worthwhile.

But as I said at the beginning, overall your solution is a fine one and a correct one.

References: