We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
Post 39
Hacker Rank 13 - Difference in 2 lists
Published on: 2025-06-12
Tags:
elixir, Side Project, Hacker Rank
This was an other fun one that didn't take too much time. We are given to lists of integers inwhich we know that list 1 will have missing numbers from list 2. We need to find the numbers that are missing from list 1 and print them (on the same line) in ascending order.
input:
10
203 204 205 206 207 208 203 204 205 206
13
203 204 204 205 206 207 205 208 203 206 205 206 204
output:
204 205 206
For this the first step was to create a map of the numbers and their counts.
def map(list) do
Enum.reduce(list, %{}, fn integer, map ->
Map.update(map, integer, 1, &(&1 + 1))
end)
end
This takes the elements of a list and returns the count of the numbers in a tuple setup.
Once we have the two maps we can then check the counts and append the any count difference to a list. Then because we need the list to be sorted... sort it.
def find_difference([map1, map2]) do
Enum.reduce(map2, [], fn {key, count2}, acc ->
count1 = Map.get(map1, key, 0)
if count2 > count1, do: [key | acc], else: acc
end)
|> Enum.sort()
end
That is about it as I always leave the output to the user.