Last active
December 15, 2020 16:52
-
-
Save yuriploc/9678fb068b8e9b2d8632c63b95d6ba96 to your computer and use it in GitHub Desktop.
Check if two lists have the same members in any order. Considers duplicated values.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def are_eq?(left, right) when is_list(left) and is_list(right), do: same_members?(left, right) | |
defp same_members?([hd_left | t_left], [_ | _] = right) do | |
if Enum.any?(right, &(&1 == hd_left)) do | |
index = Enum.find_index(right, &(&1 == hd_left)) | |
{_, next_right} = List.pop_at(right, index) | |
same_members?(t_left, next_right) | |
else | |
false | |
end | |
end | |
defp same_members?([], []), do: true | |
defp same_members?([], [_ | _]), do: false | |
defp same_members?([_ | _], []), do: false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment