# Get shortest/longest string in array

**URL:** https://discourse.julialang.org/t/get-shortest-longest-string-in-array/53739
**Category:** New to Julia
**Tags:** question
**Created:** [January 21, 2021, 6:26pm UTC](https://discourse.julialang.org/t/get-shortest-longest-string-in-array/53739 "2021-01-21T18:26:37Z")
**Posts on this page:** 1
**Showing post:** 10

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [January 21, 2021, 11:49pm UTC](https://discourse.julialang.org/t/get-shortest-longest-string-in-array/53739/10 "2021-01-21T23:49:23Z")

</div>

The naive implementation is actually hard to beat:

```julia
function minmax_elements(arr)
    minl = maxl = arr[1]
    smin = smax = sizeof(minl)
    for a in arr
        sa = sizeof(a)
        if sa > smax
            maxl, smax = a, sa
        elseif sa < smin
            minl, smin = a, sa
        end
    end
    minl, maxl
end 

arr = ["hello", "beautiful", "world", "!"];
@btime minmax_elements($arr)
  5.700 ns (0 allocations: 0 bytes)
("!", "beautiful")

```

---

_[View the full topic](https://discourse.julialang.org/t/get-shortest-longest-string-in-array/53739)._
