If you want it to output in exactly the same output as you stated (so the same order as the original) then I’d just do this:
╰─➤ iex
Erlang/OTP 20 [erts-9.1] [source] [64-bit] [smp:2:2] [ds:2:2:10] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (1.6.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> array = [%{count: 1, img: 1}, %{count: 1, img: 2}, %{count: 3, img: 3}, %{count: 1, img: 4}]
[
%{count: 1, img: 1},
%{count: 1, img: 2},
%{count: 3, img: 3},
%{count: 1, img: 4}
]
iex(2)> {_, array} = Enum.reduce(Enum.reverse(array), {nil, []}, fn
...(2)> %{count: count} = v, {c, a} when count < c -> {count, [v]}
...(2)> %{count: count} = v, {count, a} -> {count, [v | a]}
...(2)> _, acc -> acc
...(2)> end)
{1, [%{count: 1, img: 1}, %{count: 1, img: 2}, %{count: 1, img: 4}]}






















