mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-13 08:01:03 +01:00
Minor changes
This commit is contained in:
parent
13a2fe09b5
commit
929568120c
1 changed files with 67 additions and 29 deletions
|
@ -14,75 +14,113 @@
|
|||
# Output:
|
||||
# [2,3]
|
||||
|
||||
def find_duplicates(array)
|
||||
require 'benchmark'
|
||||
|
||||
array = [4, 3, 2, 7, 8, 2, 3, 1]
|
||||
long_array = [4, 3, 2, 7, 8, 2, 3, 1] * 100
|
||||
|
||||
#
|
||||
# Approach 1: Brute force
|
||||
#
|
||||
|
||||
#
|
||||
# Complexity Analysis
|
||||
#
|
||||
# Time complexity: O(n^2) average case.
|
||||
#
|
||||
|
||||
def find_duplicates(array)
|
||||
current_num = array[0]
|
||||
result_array = []
|
||||
array.each_with_index do |num, i|
|
||||
array.each_with_index do |num, j|
|
||||
if i != j && current_num == array[j]
|
||||
result_array.push(current_num)
|
||||
end
|
||||
|
||||
array.count.times do |i|
|
||||
array.count.times do |j|
|
||||
result_array.push(current_num) if i != j && current_num == array[j]
|
||||
end
|
||||
current_num = array[i+1]
|
||||
|
||||
current_num = array[i + 1]
|
||||
end
|
||||
|
||||
result_array.uniq
|
||||
end
|
||||
|
||||
array = [4,3,2,7,8,2,3,1]
|
||||
long_array = [4,3,2,7,8,2,3,1]*100
|
||||
|
||||
require 'benchmark'
|
||||
Benchmark.bmbm do |x|
|
||||
x.report('execute algorithm') do
|
||||
print find_duplicates(long_array)
|
||||
x.report('execute algorithm 1') do
|
||||
print(find_duplicates(array))
|
||||
print(find_duplicates(long_array))
|
||||
end
|
||||
end
|
||||
|
||||
def find_duplicates_2(array)
|
||||
#
|
||||
# Approach 2: Sort and Compare Adjacent Elements
|
||||
#
|
||||
|
||||
# Intuition
|
||||
|
||||
# After sorting a list of elements, all elements of equivalent value get placed together.
|
||||
# Thus, when you sort an array, equivalent elements form contiguous blocks.
|
||||
|
||||
#
|
||||
# Complexity Analysis
|
||||
#
|
||||
# Time complexity: O(n log n)
|
||||
#
|
||||
|
||||
def find_duplicates_2(array)
|
||||
sorted_array = array.sort
|
||||
result_array = []
|
||||
|
||||
(1..sorted_array.count).each do |i|
|
||||
if sorted_array[i] == sorted_array[i-1]
|
||||
result_array.push(sorted_array[i])
|
||||
end
|
||||
result_array.push(sorted_array[i]) if sorted_array[i] == sorted_array[i - 1]
|
||||
end
|
||||
|
||||
result_array.uniq
|
||||
end
|
||||
|
||||
require 'benchmark'
|
||||
Benchmark.bmbm do |x|
|
||||
x.report('execute algorithm') do
|
||||
print find_duplicates_2(long_array)
|
||||
x.report('execute algorithm 2') do
|
||||
print(find_duplicates(array))
|
||||
print(find_duplicates(long_array))
|
||||
end
|
||||
end
|
||||
|
||||
#
|
||||
# Approach 3: Hash map
|
||||
#
|
||||
|
||||
#
|
||||
# Complexity Analysis
|
||||
#
|
||||
# Time complexity: O(n) average case.
|
||||
#
|
||||
|
||||
def find_duplicates_3(array)
|
||||
result_hash = {}
|
||||
result_array = []
|
||||
# loop through array and build a hash with counters
|
||||
|
||||
# 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?
|
||||
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|
|
||||
if v > 1
|
||||
result_array.push(k)
|
||||
end
|
||||
result_array.push(k) if v > 1
|
||||
end
|
||||
|
||||
# return keys
|
||||
result_array
|
||||
end
|
||||
|
||||
require 'benchmark'
|
||||
Benchmark.bmbm do |x|
|
||||
x.report('execute algorithm') do
|
||||
print find_duplicates_3(array)
|
||||
x.report('execute algorithm 3') do
|
||||
print(find_duplicates(array))
|
||||
print(find_duplicates(long_array))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue