13

I have two sorted lists as below:

var list1 = new List<int>() { 1, 1, 1, 2, 3 };
var list2 = new List<int>() { 1, 1, 2, 2, 4 };

I want the output to be: {1, 1, 2}

How to do this in C#? Is there a way using Linq?

leppie
  • 115,091
  • 17
  • 196
  • 297
Manoj Pandey
  • 257
  • 1
  • 4
  • 12

5 Answers5

46

Use Intersect:

 var commonElements = list1.Intersect(list2).ToList();
Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
5

The extra 1 means you can't use Intersect because it returns a set.

Here's some code that does what you need:

var list1 = new List<int>() { 1, 1, 1, 2, 3 };
var list2 = new List<int>() { 1, 1, 2, 2, 4 };

var grouped1 =
    from n in list1
    group n by n
    into g
    select new {g.Key, Count = g.Count()};

var grouped2 =
    from n in list2
    group n by n
    into g
    select new {g.Key, Count = g.Count()};

var joined =
    from b in grouped2
    join a in grouped1 on b.Key equals a.Key
    select new {b.Key, Count = Math.Min(b.Count, a.Count)};

var result = joined.SelectMany(a => Enumerable.Repeat(a.Key, a.Count));

CollectionAssert.AreEquivalent(new[] {1, 1, 2}, result);
Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
3

This works nicely:

var list1 = new List<int>() { 1, 1, 1, 2, 3 };
var list2 = new List<int>() { 1, 1, 2, 2, 4 };

var lookup1 = list1.ToLookup(x => x);
var lookup2 = list2.ToLookup(x => x);

var results = lookup1.SelectMany(l1s => lookup2[l1s.Key].Zip(l1s, (l2, l1) => l1));
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • 1
    Should be the accepted solution. While @Austin Salonen answer works, this solution is far more elegant. More concise and _relatively_ easier to understand. – Ifrit Feb 18 '19 at 15:49
0

While both @Austin Salonen's solution and @Enigmativity's solution work for any given lists, neither take advantage of OP's condition that the lists are sorted.

Given that both lists will be ordered we can do a search in O(n + m) time where n and m are the length of each list. Not entirely sure what the previous solutions big o performance is, but it's definitely slower then O(n + m).

Basically we just walk both lists, moving one or both enumerators based on a comparison check.

var results = new List<int>();
var e1 = list1.GetEnumerator();
var e2 = list2.GetEnumerator();
var hasNext = e1.MoveNext() && e2.MoveNext();

while (hasNext) {
    var value1 = e1.Current;
    var value2 = e2.Current;

    if (value1 == value2) {
        results.Add(value1);
        hasNext = e1.MoveNext() && e2.MoveNext();
    } else if (value1 < value2) {
        hasNext = e1.MoveNext();
    } else if (value1 > value2) {
        hasNext = e2.MoveNext();
    }
}

That's it! results will be an empty list if no matches are found. Note this assumes both lists are in ascending order. If it's descending, just flip the < and > operators.

Ifrit
  • 6,791
  • 8
  • 50
  • 79
-2

I am late in answering this question, this might help future visitors.

            List<int> p = new List<int> { 1, 1, 1, 2, 3 };
            List<int> q = new List<int> { 1, 1, 2, 2, 4 };
            List<int> x = new List<int>();
            for (int i = 0; i < p.Count; i++ )
            {
                if (p[i] == q[i])
                {
                    x.Add(p[i]);
                }
            }
Harminder
  • 141
  • 1
  • 8
  • 1
    This doesn't answer the question. It's just a fluke that the positions match. The OP is after the occurrences not the positions. – Enigmativity Oct 09 '16 at 13:17