mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-14 08:01:05 +01:00
Minor changes
This commit is contained in:
parent
13a2fe09b5
commit
929568120c
1 changed files with 67 additions and 29 deletions
|
@ -14,52 +14,90 @@
|
||||||
# Output:
|
# Output:
|
||||||
# [2,3]
|
# [2,3]
|
||||||
|
|
||||||
def find_duplicates(array)
|
require 'benchmark'
|
||||||
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
|
|
||||||
end
|
|
||||||
current_num = array[i+1]
|
|
||||||
end
|
|
||||||
result_array.uniq
|
|
||||||
end
|
|
||||||
|
|
||||||
array = [4, 3, 2, 7, 8, 2, 3, 1]
|
array = [4, 3, 2, 7, 8, 2, 3, 1]
|
||||||
long_array = [4, 3, 2, 7, 8, 2, 3, 1] * 100
|
long_array = [4, 3, 2, 7, 8, 2, 3, 1] * 100
|
||||||
|
|
||||||
require 'benchmark'
|
#
|
||||||
|
# Approach 1: Brute force
|
||||||
|
#
|
||||||
|
|
||||||
|
#
|
||||||
|
# Complexity Analysis
|
||||||
|
#
|
||||||
|
# Time complexity: O(n^2) average case.
|
||||||
|
#
|
||||||
|
|
||||||
|
def find_duplicates(array)
|
||||||
|
current_num = array[0]
|
||||||
|
result_array = []
|
||||||
|
|
||||||
|
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]
|
||||||
|
end
|
||||||
|
|
||||||
|
result_array.uniq
|
||||||
|
end
|
||||||
|
|
||||||
Benchmark.bmbm do |x|
|
Benchmark.bmbm do |x|
|
||||||
x.report('execute algorithm') do
|
x.report('execute algorithm 1') do
|
||||||
print find_duplicates(long_array)
|
print(find_duplicates(array))
|
||||||
|
print(find_duplicates(long_array))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# 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)
|
def find_duplicates_2(array)
|
||||||
sorted_array = array.sort
|
sorted_array = array.sort
|
||||||
result_array = []
|
result_array = []
|
||||||
|
|
||||||
(1..sorted_array.count).each do |i|
|
(1..sorted_array.count).each do |i|
|
||||||
if sorted_array[i] == sorted_array[i-1]
|
result_array.push(sorted_array[i]) if sorted_array[i] == sorted_array[i - 1]
|
||||||
result_array.push(sorted_array[i])
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
result_array.uniq
|
result_array.uniq
|
||||||
end
|
end
|
||||||
|
|
||||||
require 'benchmark'
|
|
||||||
Benchmark.bmbm do |x|
|
Benchmark.bmbm do |x|
|
||||||
x.report('execute algorithm') do
|
x.report('execute algorithm 2') do
|
||||||
print find_duplicates_2(long_array)
|
print(find_duplicates(array))
|
||||||
|
print(find_duplicates(long_array))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# Approach 3: Hash map
|
||||||
|
#
|
||||||
|
|
||||||
|
#
|
||||||
|
# Complexity Analysis
|
||||||
|
#
|
||||||
|
# Time complexity: O(n) average case.
|
||||||
|
#
|
||||||
|
|
||||||
def find_duplicates_3(array)
|
def find_duplicates_3(array)
|
||||||
result_hash = {}
|
result_hash = {}
|
||||||
result_array = []
|
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
|
# where the key is the array element and the counter is the value
|
||||||
# increase counter when duplicate is found
|
# increase counter when duplicate is found
|
||||||
|
@ -70,19 +108,19 @@ def find_duplicates_3(array)
|
||||||
result_hash[num] += 1
|
result_hash[num] += 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# loop through hash and look for values > 1
|
# loop through hash and look for values > 1
|
||||||
result_hash.each do |k, v|
|
result_hash.each do |k, v|
|
||||||
if v > 1
|
result_array.push(k) if v > 1
|
||||||
result_array.push(k)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# return keys
|
# return keys
|
||||||
result_array
|
result_array
|
||||||
end
|
end
|
||||||
|
|
||||||
require 'benchmark'
|
|
||||||
Benchmark.bmbm do |x|
|
Benchmark.bmbm do |x|
|
||||||
x.report('execute algorithm') do
|
x.report('execute algorithm 3') do
|
||||||
print find_duplicates_3(array)
|
print(find_duplicates(array))
|
||||||
|
print(find_duplicates(long_array))
|
||||||
end
|
end
|
||||||
end
|
end
|
Loading…
Reference in a new issue