Skip to content

Instantly share code, notes, and snippets.

@mykhailokrainik
Created July 6, 2018 15:46
Show Gist options
  • Save mykhailokrainik/a71a5687b0d60cd3542b3140085e15a1 to your computer and use it in GitHub Desktop.
Save mykhailokrainik/a71a5687b0d60cd3542b3140085e15a1 to your computer and use it in GitHub Desktop.
Implement the uniq method to remove all duplicates.
def uniq(arr)
r = [arr[0]]
arr.each do |a|
uniq = true
r.each do |n|
if a == n
uniq = false
end
end
r.push a if uniq
end
r
end
@mykhailokrainik
Copy link
Author

describe 'Algorithm' do
  subject { uniq([2, 45, 7, 45, 11, -7, 8, 7, 992]) }
  it '#uniq' do
    is_expected.to eq [2, 45, 7, 11, -7, 8, 992]
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment