aboutsummaryrefslogtreecommitdiff
path: root/challenge-283/packy-anderson/elixir/ch-1.exs
blob: 98c5df8b921fe514d7bdfabf209346eaa4240dfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env elixir

defmodule PWC do
  @doc """
  Helper function that accepts a list and generates a Multiset/
  Bag implemented as a Map.

  https://en.wikipedia.org/wiki/Multiset
  """
  def makeBag(list) do
    {_, bag} = Enum.map_reduce(list, %{}, fn i, bag ->
      { i, Map.put(bag, i, Map.get(bag, i, 0) + 1) }
    end)
    bag
  end

  @doc """
  You are given an array of integers where every element
  appears more than once except one element.

  Find the one element that appears exactly one time.
  """
  def uniqueNumber(ints) do
    keys = ints
    |> makeBag
    |> Map.filter(fn {_, v} -> v == 1 end) # keys that occur once
    |> Map.keys # return just the keys from the map
    cond do
      Kernel.length(keys) == 0 ->
        "no element appears only once"
      Kernel.length(keys) > 1 ->
        "multiple elements appear only once"
      true ->
        keys |> List.first |> to_string
    end
  end

  @doc """
  Function to print the input, invoke the solution code,
  and print the output.
  """
  def solution(ints) do
    IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")")
    IO.puts("Output: " <> uniqueNumber(ints) )
  end
end

IO.puts("Example 1:")
PWC.solution([3, 3, 1])

IO.puts("\nExample 2:")
PWC.solution([3, 2, 4, 2, 4])

IO.puts("\nExample 3:")
PWC.solution([1])

IO.puts("\nExample 4:")
PWC.solution([4, 3, 1, 1, 1, 4])

IO.puts("\nInvalid Input 1 (no element appears only once):")
PWC.solution([4, 1, 1, 1, 4])

IO.puts("\nInvalid Input 2 (multiple elements appear only once):")
PWC.solution([1, 2, 3, 4])