Created
June 25, 2025 18:23
-
-
Save alexismansilla/e0f3e1c9db557410f2d445b8c4589bdb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Test | |
# [2, 1, 3, 5, 3, 2] | |
def performance(array) O(n) | |
hash = {} | |
array.each do |element| # 2 | |
if hash[element] # {} , { 2 => true }, { 2 => true, 1 => true }, { 2 => true, 1 => true, 3 => true} ... | |
return element # => si existe retorna 3 | |
else | |
hash[element] = true | |
end | |
end | |
-1 | |
end | |
def main # O(n2) | |
array = [2, 1, 3, 5, 3, 2] | |
dupli = [] | |
array.each_with_index do |element, index| | |
if array.index(element) != index && !dupli.include?(element) | |
dupli << element | |
end | |
end | |
if dupli.empty? | |
return -1 | |
end | |
return dupli[0] | |
end | |
end | |
RSpec.describe '#performance' do | |
it 'input [2, 1, 3, 5, 3, 2]' do | |
expect(Test.new.performance([2, 1, 3, 5, 3, 2])).to eq(3) | |
end | |
it 'input [2, 4, 3, 5, 1]' do | |
expect(Test.new.performance([2, 4, 3, 5, 1])).to eq(-1) | |
end | |
it 'input [1]' do | |
expect(Test.new.performance([1])).to eq(-1) | |
end | |
it 'input [2, 2]' do | |
expect(Test.new.performance([2, 2])).to eq(2) | |
end | |
it 'input [2, 1]' do | |
expect(Test.new.performance([2, 1])).to eq(-1) | |
end | |
it 'input [2, 1, 3]' do | |
expect(Test.new.performance([2, 1, 3])).to eq(-1) | |
end | |
it 'input [2, 3, 3]' do | |
expect(Test.new.performance([2, 3, 3])).to eq(3) | |
end | |
it 'input [3, 3, 3]' do | |
expect(Test.new.performance([3, 3, 3])).to eq(3) | |
end | |
it 'input [8, 4, 6, 2, 6, 4, 7, 9, 5, 8]' do | |
expect(Test.new.performance([8, 4, 6, 2, 6, 4, 7, 9, 5, 8])).to eq(6) | |
end | |
it 'input [10, 6, 8, 4, 9, 1, 7, 2, 5, 3]' do | |
expect(Test.new.performance([10, 6, 8, 4, 9, 1, 7, 2, 5, 3])).to eq(-1) | |
end | |
it 'input [1, 1, 2, 2, 1]' do | |
expect(Test.new.performance([1, 1, 2, 2, 1])).to eq(1) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment