Assume that you have two IEnumerbale
objects. How we can merge them (in some condition e.g merge in merge sort ...) and create a unique IEnumerable
? I tried this with Zip
, but in Zip the two list sizes should be equal (maybe you didn't get exception but maybe we have some data lost.)
In addition, I try it by using Enumerable.Range(...).Select(...) but i didn't get an acceptable result.
Furthermore, my question is totally different from using Union or this one, in fact as I said like merge in merge sort I like to preserve lists order (in fact just want to fill some gaps in first list).
It's easy to handle it with for loop, but i can't see any full linq way.
Edit:
Sample input:
lst1 = {5,10,12}
lst2 = {7,9,16,20,25}
result: {5,7,9,10,12,16,20,25}
this can be done with a for loop and two pointer in O(n + m)
but I'm looking for linq solution in O(n+m)
for loop solution:
var lst1 = new List<int> { 5, 10, 12 };
var lst2 = new List<int> { 7, 9, 16, 20, 25 };
var result = new List<int>();
int j = 0;
for (int i = 0; i < lst1.Count; i++)
{
while (j < lst2.Count && lst2[j] < lst1[i])
{
result.Add(lst2[j]);
j++;
}
result.Add(lst1[i]);
}
while (j < lst2.Count)
{
result.Add(lst2[j]);
j++;
}
Console.WriteLine(string.Join(",", result.ToArray()));
See Question&Answers more detail:os