DEV Community

Discussion on: LOC is an important metric to measure developer productivity

Collapse
 
thepeoplesbourgeois profile image
Josh • Edited

EDIT: After thinking about the problem a little longer, I realized there was an even faster and more memory efficient implementation of the method, so I added it to the end of the list.

Which of these deduplicate methods is "laziest"?

def loop_deduplicate(array)
  new_array = []
  array.each do |item|
    if new_array.include?(item)
      # no-op
    else
      new_array.push(item)
    end
  end
  new_array
end

# hashes in Ruby retain insertion order as of version 1.9
def hash_deduplicate(array)
  array.reduce({}) do |hash, item|
    hash[item] = 1
    hash
  end.keys
end

# Ruby allows for one-line method definitions as of version 3.0
def union_deduplicate(array) = array | array
Enter fullscreen mode Exit fullscreen mode

EDIT 2 (and hint!): As the O.G. edit should imply, this is very much a trick question!