Ad
  • Custom User Avatar

    This is a very compact solution, but this is actually not the best approach. Looking closely at the code this is generating an array for each key witht the default value increasing its space complexity based on the input of n, keys in this case, so if you have 1 million keys, this code creates 1 million array objects before it can be converted into the final hash. Array are complex objects that takes fair amount of space (memory). besides this fact this code is also complex to read for anyone who is not fairly intermediate level or higher in ruby.

    One liners are elegant looking code but we have to think of tradeoffs when making such choices.

  • Custom User Avatar

    I used inject, but this seems more compact. Can anyone explain how this works?