r/dailyprogrammer • • May 16 '12

[5/16/2012] Challenge #53 [easy]

Write a function that given two sorted lists, returns a list whith the two lists merged together into one sorted list.

So, for instance, for inputs [1,5,7,8] and [2,3,4,7,9] it should return [1,2,3,4,5,7,7,8,9].

Try and make your code as efficient as possible.

19 Upvotes

39 comments sorted by

View all comments

Show parent comment

1

u/bs_detector May 17 '12 edited May 17 '12

I had this, sans LINQ, but yours is better. My code runs a million times in 600 ms on a Mac Book Pro i7 2.67 Ghz. Pretty tight.

var l1 = new int[] {1, 5, 7, 8};
var l2 = new int[] {2, 3, 4, 7, 9};

var list = l1.Concat(l2).ToList();
list.Sort();
list.ForEach(Console.WriteLine);