How can I improve the code to run it less than o(n2) time?

hash = {
‘Videos’ => [‘avi’, ‘wmv’, ‘vob’, ‘swf’, ‘rm’, ‘mpg’, ‘mov’,
‘mp4’],
‘Songs’ => [‘mp3’, ‘wma’, ‘wav’, ‘au’],‘Text’ => [‘txt’],
‘Binary’ => [‘bin’],
‘Documents’ => [‘pdf’, ‘ppt’, ‘docx’, ‘doc’, ‘odt’, ‘rtf’]
}
array = [
[“How To - Manage Accounts”, 3528877, “mp4”], [“Quick Guide”,
3103717, “pdf”],
[“How To - Upload Files”, 2496476, “mp4”], [“Data Sheet -
Security”, 735548, “pdf”],
[“How To - Share Folders”, 11039700, “mp4”],
[“How To - Collaborate”, 4750831, “mp4”], [“Data Sheet -
Platform”, 394739, “pdf”]
]

array_out = array.map do |a|
a.fill(hash.find { |k,v| v.include?(a.last) }.first,-1,1)
end

p array_out

output :

[[“How To - Manage Accounts”, 3528877, “mp4”], [“Workshare Quick Start
Guide”, 3
103717, “pdf”], [“How To - Upload Files”, 2496476, “mp4”], ["Data Sheet

  • Worksh
    are Data Security, Policy Enforcement, and Data Center Architecture",
    735548, “p
    df”], [“How To - Share Folders”, 11039700, “mp4”], [“How To -
    Collaborate”, 4750
    831, “mp4”], [“Data Sheet - Workshare Platform”, 394739, “pdf”]]

The above is working. Can this be written in less than o(n2) time ?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs