Get source implementation code of the entire .NET Framework. See how the experts code at Microsoft.
Below URL is base code for Arrays implementation in .NET Framework.
https://referencesource.microsoft.com/#mscorlib/system/array.cs,42e9b7616956cf94
Get source implementation code of the entire .NET Framework. See how the experts code at Microsoft.
Below URL is base code for Arrays implementation in .NET Framework.
https://referencesource.microsoft.com/#mscorlib/system/array.cs,42e9b7616956cf94
From : Home Depot Survey <infogxxxxxxxxxxxxxx@xxxxxxxxxxxxgmail.mcsv.net> Subject : Re: Open Immediately |
As in my previous post, i dismiss all the Stack Overflow solutions with live code, and I presented my 1st attempt at the solution.
This post demonstrates my super fast implementation, enjoy!
//UNORDERED, NOT UNIQUE lists, lists with duplicates
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" };
//DESIRED RESULT
2,5,5,5,7,7,7,9 OR 2,5,5,5,9,7,7,7 OR 9,7,7,7,2,5,5,5 //UNORDERED IS SUFFICIENT
NOTE: Linq Except but it's designed as a set command, and as a consequence it removes duplicates.
IEnumerable<string> AminuBdifference = arrA.Except(arrB); //removes dupsIEnumerable<string> Uniondifference = arrA.Union(arrb); //ideally, but no good because dups are removed
See https://dotnetfiddle.net/oHOms5 which demonstrates Linq Except use.
Below is my super fast implementation, using a pure BitArray implementations, enjoy!
Source Code
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Diagnostics; //CHALLENGE - Get difference between two unorderd, not unique lists, duplicates allowed within a list and across both lists. //SOLUTION - 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 //OPTIMIZE - THIS IS IT using BitArray - https://metadataconsulting.blogspot.com/2021/02/CSharp-dotNet-Get-difference-between-two-unordered-not-unique-lists-lightning-fast-optimized.html //OPTIMIZEv2 - All BitArray imp - https://metadataconsulting.blogspot.com/2021/02/CSharp-dotNet-Get-difference-between-two-unordered-not-unique-lists-using-BitArrays-for-super-fast-speed.html public class Program { public static int loopcnt = 0; public static void Main() { Stopwatch sw = new Stopwatch(); sw.Start(); //RANDOM case - typical use string[] arrA = new string[] { "7", "1", "2", "3", "4", "5", "9", "7", "7", "1" }; string[] arrB = new string[] { "1", "1", "2", "2", "3", "4", "5", "5", "5", "5" }; //WANT 2,5,5,5,9,7,7,7 //WORST case - entirely different, but have 1 value in common to make it interesting //string[] arrA = new string[] { "1", "1", "2", "3", "4", "5", "6", "7", "7", "7" };//1st list //string[] arrB = new string[] { "8", "9", "10", "12", "1", "14", "15", "15", "11", "9" };//2nd list //BEST case - identical //string[] arrA = new string[] { "1", "1", "2", "3", "4", "5", "6", "7", "7", "7" };//1st list //string[] arrB = new string[] { "1", "1", "2", "3", "4", "5", "6", "7", "7", "7" };//2nd list sw.Stop(); Console.WriteLine("RANDOM CASE -two unorderd, not unique lists "); Console.WriteLine("A = " + string.Join(",", arrA)); Console.WriteLine("B = " + string.Join(",", arrB)); sw.Restart(); string[] arrBminusA = new string[arrB.Length]; string[] arrAminuB = new string[arrA.Length]; BitArray bitArrAminusB = new BitArray(arrA.Length); BitArray bitarrBminusA = new BitArray(arrB.Length); //May seem counter intuitive to run this over 3 loop but it's faster ~ 10 ticks int a = 0; for (int b = 0; b < arrB.Length; b++) { for (a = 0; a < arrA.Length; a++) { loopcnt++;//not need for timing analysis if (!bitArrAminusB[a] && arrA[a] == arrB[b]) { bitArrAminusB.Set(a, true); //A-B, list A difference/subtract of list B bitarrBminusA.Set(b, true); //B-A, list B difference/subtract of list A break; } } } int idxa = 0; for (int aa = 0; aa < arrA.Length; aa++) { if (!bitArrAminusB[aa]) arrAminuB[idxa++] = arrA[aa]; } int idxb = 0; for (int bb = 0; bb < arrB.Length; bb++) { if (!bitarrBminusA[bb]) arrBminusA[idxb++] = arrB[bb]; } sw.Stop(); Console.WriteLine("(A-B) Result = " + string.Join(",", arrAminuB).Trim(',')); //Console.WriteLine("D = " + string.Join(",", D)); //Console.WriteLine("E = " + string.Join(",", E)); Console.WriteLine("(B-A) Result = " + string.Join(",", arrBminusA).TrimEnd(',')); Console.WriteLine("No. of loops = " + loopcnt); Console.WriteLine("No. of loops = " + arrA.Length*2 + " - Post copy using BitArray to a sized array"); //Un Console.WriteLine("Result Unorderd Final A then B = " + (string.Join(",", arrAminuB)).Trim(',') + "," + string.Join(",", arrBminusA).Trim(',') + " in " + sw.ElapsedTicks + " ticks."); Console.WriteLine("Result Unorderd Final B then A = " + (string.Join(",", arrBminusA)).Trim(',') + "," + string.Join(",",arrAminuB).Trim(',') + " in " + sw.ElapsedTicks + " ticks."); List<string> D = arrBminusA.ToList().Concat(arrAminuB.ToList()).ToList(); D.Sort(); Console.WriteLine("Result Ordered Final = " + string.Join(",", D).TrimStart(',')); Console.WriteLine("Result O(n^2+2n) but very fast - can you beat it?"); } }