Commit 8844865

mo khan <mo.khan@gmail.com>
2020-08-18 22:00:52
linear algorithm to find item that does not have a match
1 parent 10e4767
Changed files (1)
2020
08
2020/08/18/main.rb
@@ -0,0 +1,22 @@
+def assert_equal(x, y)
+  raise [x, y].inspect unless x == y
+end
+
+class Solution
+  # time: O(n)
+  # space: O(n)
+  def self.run(items)
+    cache = Hash.new do |hash, key|
+      hash[key] = 2
+    end
+
+    for i in (0...items.size)
+      item = items[i]
+      cache[item] -= 1
+    end
+
+    cache.values.find { |value| value > 0 }
+  end
+end
+
+assert_equal 1, Solution.run([4, 3, 2, 4, 1, 3, 2])