C # Merging two lists with overlapping data

If I have 2 line lists

List<string> history = new List<string>(){ "AA", "BB", "CC", "AA" };
List<string> potentialNew = new List<string>(){ "CC", "AA", "DD", "EE", "FF", "AA"};

I need a way to combine lists, preventing "overlapping" and keeping the same order. So, in the example above there will be a combined list:

AA, BB, CC, AA, DD, EE, FF, AA

In other words, historyonly DD, EE, FF, and AA are added to the list .

I tried to figure this out for several days, and countless searches did not give any solution. Any help would be greatly appreciated!

+4
source share
5 answers

This will give you the expected result for typing the given input, as you mentioned in the question:

 List<string> history = new List<string>() { "AA", "BB", "CC", "AA" };
 List<string> potentialNew = new List<string>() { "CC", "AA", "DD", "EE", "FF" };
 var result = history.Concat(potentialNew.Where(x => !history.Contains(x)).ToList());

.Concat() . potentialNew, , .

: , , - :

string lastItem = history.Last();
   int lastIndexToCheck=history.Count-2,i=0;
   for (; i < potentialNew.Count - 1; i++)
       {
          if (potentialNew[i] == lastItem && potentialNew[i - 1] == history[lastIndexToCheck])
              {
                 break;
              }
       }
       history.AddRange(potentialNew.Skip(i+1).ToList());  

.

+5
using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        List<string> history = new List<string>(){ "AA", "BB", "CC", "AA" };
        List<string> potentialNew = new List<string>(){ "CC", "AA", "DD", "EE", "FF" };
        // make lists equal length

        foreach(var x in history.ConcatOverlap(potentialNew)){
            Console.WriteLine(x);
        }
    }


}

public static class Ext{
    public static IEnumerable<string> ConcatOverlap(this List<string> history, List<string> potentialNew){
        var hleng = history.Count();
        var pleng = potentialNew.Count();
        if(pleng > hleng) history = history.Concat(Enumerable.Range(1, pleng - hleng).Select(x => string.Empty)).ToList();
        if(hleng > pleng) potentialNew = Enumerable.Range(1, hleng - pleng).Select(x => string.Empty).Concat(potentialNew).ToList();


        var zipped = history.Zip(potentialNew, (a,b)=> new {First=a,Next=b, Equal = (a.Equals(b) || string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b))});
        var count = 0;
        var max = pleng > hleng ? pleng : hleng;
        Console.WriteLine("Max " + max);
        while(zipped.Any(x => !x.Equal) && count < max - 1){
            count++;
            potentialNew.Insert(0,string.Empty);
            history.Add(string.Empty);
            zipped = history.Zip(potentialNew, (a,b)=> new {First=a,Next=b, Equal = (a.Equals(b) || string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b))});
        }
        return zipped.Select(x => string.IsNullOrEmpty(x.First) ? x.Next : x.First);
    }
}

:

public static IEnumerable<T> ConcatOverlap<T>(this IEnumerable<T> head, IEnumerable<T> tail){

    var skip = 0;
    var hLen = head.Count();
    while(head.Skip(skip).Zip(tail, (a,b) => a.Equals(b)).Any(x => !x) && skip < hLen){
        skip++;
    }

    return head.Take(skip).Concat(tail);
}
+1
var history = new List<string>() { "AA", "BB", "CC", "AA" };
var potentialNew = new List<string>() { "CC", "AA", "DD", "EE", "FF" };

// Get the min. number of items to compare that 2 lists
for (int count = Math.Min(history.Count(), potentialNew.Count()); count >= 0; count--)
{
    // Get the items from the back of history list, and get the items from potential list
    // Compare them by SequenceEqual()
    if (history.Skip(history.Count() - count).Take(count).SequenceEqual(potentialNew.Take(count)))
    {
        // Add the items to the result if found. It must be the greatest common part
        return history.Concat(potentialNew.Skip(count));
    }
}

.NET Fiddle

+1

:

List<string> history = new List<string>(){ "AA", "BB", "CC", "AA" };
List<string> potentialNew = new List<string>(){ "CC", "AA", "DD", "EE", "FF", "AA"};

potentialNew.Aggregate(history, (h, p) =>
{
    if (!h.Skip(h.Count - 2).Contains(p))
    {
        h.Add(p);
    }
    return h;
});

history :

AA 
BB 
CC 
AA 
DD 
EE 
FF 
AA 
0

, , , . , .

List<string> history = new List<string>() { "AA", "BB", "CC", "AA" };
List<string> potentialNew = new List<string>() { "CC", "AA", "DD", "EE", "FF", "AA" };

var result = ProcessChatLog(history,potentialNew);
//pass these two list to a function to process the chat log

.

 public List<string> ProcessChatLog(List<string> history, List<string> potentialNew)
    {
        var lastChat = history.Last();  
        var lastChatIndex = history.Count - 1;
        var allIndexWithLastChat = potentialNew.Select((c, i) => new { chat = c, Index = i })
                                   .Where(x => x.chat == lastChat)
                                   .Select(x => x.Index).Reverse().ToList();       

        List<int> IndexToClear = new List<int>();
        bool overlapFound = false;

        foreach (var index in allIndexWithLastChat)
        {
            if (!overlapFound)
            {
                int hitoryChatIndex = lastChatIndex;
                IndexToClear.Clear();
                for (int i = index; i > -1; i--)
                {
                    if (potentialNew[i] == history[hitoryChatIndex])
                    {
                        IndexToClear.Add(i);
                        if (i == 0)
                        {
                            overlapFound = true;
                            break;
                        }
                        hitoryChatIndex--;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            else
            {
                IndexToClear.Clear();
                break;
            }                             
        }

        if(IndexToClear.Count >0)
        {
            potentialNew.RemoveRange(IndexToClear.Min(), IndexToClear.Count);   
        }

        return history.Concat(potentialNew).ToList();
    }

  history = { "AA", "BB", "CC", "AA" }
  potentialNew = { "CC", "AA", "DD", "EE", "FF", "AA"}

  Result = { "AA", "BB","CC", "AA", "DD", "EE", "FF", "AA"}

  history = { "AA", "BB","AA", "CC", "AA" }
  potentialNew = { "AA","CC", "AA", "DD", "EE", "FF", "AA"}

  Result = { "AA", "BB","AA","CC", "AA", "DD", "EE", "FF", "AA"}

  history = { "AA", "BB", "CC", "AA" }
  potentialNew = { "CC", "AA", "CC", "AA", "FF", "AA"}

  Result = { "AA", "BB","CC", "AA", "CC", "AA", "FF", "AA"}

  history = { "AA", "BB", "CC", "AA" }
  potentialNew = {"AA", "CC", "AA", "DD", "EE", "FF", "AA" }

  Result = { "AA", "BB","CC", "AA", "CC", "AA", "DD", "EE", "FF", "AA" }

, .

, , . , 20 , 2 11 9 . , , 9 . , , , , , , . ,

0

All Articles