Skip to content

Instantly share code, notes, and snippets.

@abrahamsangha
Last active January 20, 2016 02:45
Show Gist options
  • Save abrahamsangha/09664d58a5527152fa10 to your computer and use it in GitHub Desktop.
Save abrahamsangha/09664d58a5527152fa10 to your computer and use it in GitHub Desktop.
# O(n^2)
def dedupe(sorted_array)
previous_values = []
sorted_array.map do |element|
if previous_values.include? element
next
else
previous_values << element
element
end
end.compact
end
# O(n)
def dedupe(sorted_array)
previous_values = []
sorted_array.map do |element|
if element == previous_values.last
next
else
previous_values << element
element
end
end.compact
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment