using System.Collections.Generic;
using System.Diagnostics;
public static class Program
public static int loopcnt = 0;
public static IEnumerable<T> RemoveRange<T>(this IEnumerable<T> source, IEnumerable<T> second)
var tempList = source.ToList();
foreach (var item in second)
public static void RemoveAt<T>(ref T[] arr, int index)
for (int a = index; a < arr.Length - 1; a++)
Array.Resize(ref arr, arr.Length - 1);
public static void Main()
Console.WriteLine("Get difference between two unordered, not unique (duplicates) lists!");
Console.WriteLine("Solution by https://metadataconsulting.blogspot.com/2021/02/CSharp-dotNet-Get-difference-between-two-unordered-not-unique-lists-aka-duplicates-allowed-within-a-list-and-across-both-lists.html");
Stopwatch sw = new Stopwatch();
string[] arrA = new string[] { "1", "1", "2", "3", "4", "5", "9", "7", "7", "7" };
string[] arrB = new string[] { "1", "1", "2", "2", "3", "4", "5", "5", "5", "5" };
Console.WriteLine("A = " + string.Join(",", arrA));
Console.WriteLine("B = " + string.Join(",", arrB));
Console.WriteLine("A Count = " + arrA.Length);
Console.WriteLine("B Count = " + arrB.Length);
string[] diff = new string[arrA.Length+arrB.Length];
for (int b = 0; b < arrB.Length; b++)
for (a = 0; a < arrA.Length; a++)
Console.WriteLine("No. of loops = " + loopcnt);
Console.WriteLine("(A-B) Result = " + string.Join(",", arrA).TrimEnd(','));
Console.WriteLine("(B-A) Result = " + string.Join(",", diff).TrimEnd(','));
Console.WriteLine("Result Unorderd Final = " + (string.Join(",", diff)).TrimEnd(',') + "," + string.Join(",", arrA) + " in " + sw.ElapsedTicks + " ticks.");
List<string> C = diff.ToList();
List<string> D = diff.ToList().Concat(arrA.ToList()).ToList();
Console.WriteLine("Result Ordered Final = " + string.Join(",", D).TrimStart(','));
Console.WriteLine("000 StackOverflow answers all wrong 000");
Console.WriteLine("https://stackoverflow.com/questions/15801655/difference-between-two-lists-preserving-duplicates");
Console.WriteLine("000 I don't post on StackOverflow because of the idiotic moderators and comments 000");
List<string> M = new List<string>() { "1", "1", "2", "3", "4", "5", "9", "7", "7", "7" };
List<string> N = new List<string>() { "1", "1", "2", "2", "3", "4", "5", "5", "5", "5" };
var ListDiffs = M.RemoveRange(N);
Console.WriteLine("RemoveRage Result = " + string.Join(",", ListDiffs) + " in " + sw.ElapsedTicks + " ticks.");
List<string> list2 = new List<string>() { "1", "1", "2", "3", "4", "5", "9", "7", "7", "7" };
List<string> list1 = new List<string>() { "1", "1", "2", "2", "3", "4", "5", "5", "5", "5" };
var lookup2 = list2.ToLookup(str => str);
var result = from str in list1
group str by str into strGroup
= Math.Max(0, strGroup.Count() - lookup2[strGroup.Key].Count())
from missingStr in strGroup.Take(missingCount)
Console.WriteLine("Lookup Result = " + string.Join(",", result) + " in " + sw.ElapsedTicks + " ticks.");
List<string> AA = new List<string>() { "1", "1", "2", "3", "4", "5", "9", "7", "7", "7" };
List<string> BB = new List<string>() { "1", "1", "2", "2", "3", "4", "5", "5", "5", "5" };
var diffLinq = AA.Select(e => new { Key = e, Val = 1 })
.Concat(BB.Select(e => new { Key = e, Val = -1 }))
.GroupBy(e => e.Key, e => e.Val)
.SelectMany(g => Enumerable.Repeat(g.Key, Math.Max(0, g.Sum())))
Console.WriteLine("diffLinq Result = " + string.Join(",", diffLinq) + " in " + sw.ElapsedTicks + " ticks.");