Skip to content

Instantly share code, notes, and snippets.

@jaredbeck
Created October 17, 2021 01:06
Show Gist options
  • Select an option

  • Save jaredbeck/edc708df10fcc0267db80bf1c31c8298 to your computer and use it in GitHub Desktop.

Select an option

Save jaredbeck/edc708df10fcc0267db80bf1c31c8298 to your computer and use it in GitHub Desktop.

Revisions

  1. jaredbeck created this gist Oct 17, 2021.
    41 changes: 41 additions & 0 deletions duplicate_columns.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    require 'benchmark'
    require 'matrix'

    v1 = [1, 1, 1, 1]
    v2 = [2, 3, 2, 3]
    v3 = [1, 1, 1, 1 ]

    def sol_1(a,b,c)
    sum = 0
    zip = a.zip(b, c)
    zip.group_by { |e| e}
    .select { |_, value| value.size > 1 }
    .each_value { |value| sum += (value.size - 1) }
    return sum
    end


    def sol_2(a,b,c)
    zip = a.zip(b, c)
    hash = Hash.new(0)
    zip.each { |e| hash.store(e, hash[e]+1) }
    hash.each{|e, _| hash[e] -= 1}
    return hash.sum {|e, _| hash[e] }
    end

    def sol_3(matrix)
    Matrix.
    columns(matrix).
    to_a.
    each_with_object({}) { |e, a|
    digest = e.hash
    a[digest] = a[digest].nil? ? 1 : a[digest] + 1
    }.sum { |_, v| v > 1 ? 1 : 0 }
    end

    n=1_000
    Benchmark.bmbm do |x|
    x.report("sol_1"){n.times{sol_1(v1, v2, v3)} }
    x.report("sol_2"){n.times{sol_2(v1, v2, v3)} }
    x.report("sol_3"){n.times{sol_3([v1, v2, v3])} }
    end