From f6a84ea326f626869b7876fff5f564b2f49a6de1 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Mon, 29 Mar 2021 15:37:02 -0700 Subject: [PATCH] Move more algos to hash table folders --- .../arrays/find_all_duplicates_in_an_array.rb | 41 ------------- .../find_all_duplicates_in_an_array.rb | 61 +++++++++++++++++++ 2 files changed, 61 insertions(+), 41 deletions(-) create mode 100644 data_structures/hash_table/find_all_duplicates_in_an_array.rb diff --git a/data_structures/arrays/find_all_duplicates_in_an_array.rb b/data_structures/arrays/find_all_duplicates_in_an_array.rb index 63e6517..7ee99ff 100644 --- a/data_structures/arrays/find_all_duplicates_in_an_array.rb +++ b/data_structures/arrays/find_all_duplicates_in_an_array.rb @@ -87,44 +87,3 @@ Benchmark.bmbm do |x| print(find_duplicates(long_array)) end end - -# -# Approach 3: Hash map -# - -# -# Complexity Analysis -# -# Time complexity: O(n) average case. -# - -def find_duplicates(array) - result_hash = {} - result_array = [] - - # loop through array and build a hash with counters - # where the key is the array element and the counter is the value - # increase counter when duplicate is found - array.each do |num| - if result_hash[num].nil? - result_hash[num] = 1 - else - result_hash[num] += 1 - end - end - - # loop through hash and look for values > 1 - result_hash.each do |k, v| - result_array.push(k) if v > 1 - end - - # return keys - result_array -end - -Benchmark.bmbm do |x| - x.report('execute algorithm 3') do - print(find_duplicates(array)) - print(find_duplicates(long_array)) - end -end diff --git a/data_structures/hash_table/find_all_duplicates_in_an_array.rb b/data_structures/hash_table/find_all_duplicates_in_an_array.rb new file mode 100644 index 0000000..bc68423 --- /dev/null +++ b/data_structures/hash_table/find_all_duplicates_in_an_array.rb @@ -0,0 +1,61 @@ +# Find All Duplicates in an Array +# +# Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), +# some elements appear twice and others appear once. +# +# Find all the elements that appear twice in this array. +# +# Could you do it without extra space and in O(n) runtime? +# +# Example: +# Input: +# [4,3,2,7,8,2,3,1] +# +# Output: +# [2,3] + +require 'benchmark' + +array = [4, 3, 2, 7, 8, 2, 3, 1] +long_array = [4, 3, 2, 7, 8, 2, 3, 1] * 100 + +# +# Approach: Hash table +# + +# +# Complexity Analysis +# +# Time complexity: O(n) average case. +# + +def find_duplicates(array) + result_hash = {} + result_array = [] + + # loop through array and build a hash with counters + # where the key is the array element and the counter is the value + # increase counter when duplicate is found + array.each do |num| + if result_hash[num].nil? + result_hash[num] = 1 + else + result_hash[num] += 1 + end + end + + # loop through hash and look for values > 1 + result_hash.each do |k, v| + result_array.push(k) if v > 1 + end + + # return keys + result_array +end + +Benchmark.bmbm do |x| + x.report('execute algorithm 3') do + print(find_duplicates(array)) + print(find_duplicates(long_array)) + end +end