, , , . , .
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);
.
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 . , , , , , , . ,