aAarrEAreCXBZCBC| G596VBCB 5659G5V~~C 5656666F55WCX

Distinct.` This removes all duplicate elements in a collection. It returns only distinct (or unique) elements. The System.Linq namespace provides this extension method.`Distinct` returns an IEnumerable collection. We can loop over the collection returned by Distinct, or invoke other extension methods upon it. `IEnumerable `ienumerable`An example.` We declare and allocate an array on the managed heap. The array contains 6 elements, but only 4 different numbers. Two are repeated. This fact is key to the program's output. `Int Array `int-array`Next: `We apply the Distinct extension method to the array reference, and then assign the result to an implicitly typed local variable.`Var `var`Finally: `We loop over the result and display the distinct elements in the processed array.`IEqualityComparer.` We can specify an IEqualityComparer to compare elements in the Distinct call. This is probably not useful in many programs. `IEqualityComparer `iequalitycomparer`We can "transform" elements in an IEqualityComparer. Here we treat each int as its parity (whether it is even or odd).`Odd, Even `odd`Benchmark duplicate methods.` Usually a simple loop can be written to remove duplicates. A nested for-loop can execute much faster than the Distinct method on an int array. `Version 1: `We use the Distinct method. Note how the code is short and easy to read. This is a benefit.`Version 2: `A nested loop scans following elements for a duplicate. An element is added only if no following elements are the same.`Result: `On a short int array, the nested loops are faster. But this will depend on the data given to the methods.`Discussion.` The Distinct method is not ideal for all purposes. Internally, the Distinct method is implemented in terms of iterators that are automatically generated by the C# compiler. `Therefore: `Heap allocations occur when you invoke Distinct. For optimum performance, you could use loops on small collections.`With small data sets, the overhead of using iterators and allocations likely overshadows any asymptotic advantage.`A summary.` We used the Distinct extension method from System.Linq. This method provides a declarative, function-oriented syntax for a typically imperative processing task.`The Distinct extension` incurs practical performance drawbacks in some programs. For performance, a for-loop is probably better (but may be harder to maintain).

XYZ175:DQDQbJA%{U8pzUy{gMyU{=M@ihpM'hDQDQ JzBii{EVKxBiiKJA%zUy{VyUq{=M@Mp'MDQDQ !JA?zzU{jwKU?zzU{jwozMyqozwiyU^{wp8ywiy}U^mUxUyGmMvUKM%zUyiyy,`wiy{MyUmM]G3y,`wiy{MyUmM]G3'0#O'0#O5ug| YY; YY.LinqYYY { XYX{Z XXYDeclare an Y with some duplicated YsYit. XXZY[]Z Y1Y{ 1, 2, 2, 3, 4, 4 };Z XXYInvoke Distinct extension mY. XXZvar YYY1.ZDistinctZ();Z XXYY Ys. XXZY (YYYY) XX{ XXXYY); XX} X} } Z 1 2 3 4Z YY; YY.Linq; Y YZEqualityParityZ : IEqualityComparer<Y> { XYbool Equals(Yx, Yy) X{Z XXYConsider all even Ys the same,Yall odd the same. XXZY (x % 2)Y(y % 2); X} XYYGetHashCode(Yobj) X{ XXY (obj % 2).GetHashCode(); X} } YY { XYX{ XXZY[]Z Y1Y{ 9, 11, 13, 15, 2, 4, 6, 8 };Z XXYThis will remove all except the first eventYodd. XXZvar distinctResultYY1.ZDistinctZ(YEqualityParity());Z XXYY Ys. XXZYZ (var YYdistinctResult) XX{ XXXYY); XX} X} } Z 9 2Z YY; YY.Linq; YY YY { XYIEnumerable<Y> ZTest1Z(Y[] Y) X{Z XXYUse distinctYcheck Yduplicates. XXZY Y.Distinct(); X} XYIEnumerable<Y> ZTest2Z(Y[] Y) X{Z XXYUse nested loopYcheck Yduplicates. XXZY<Y> YYYY<Y>(); XXY(YiY0; i < Y.LY; i++) XX{Z XXXYCheck YduplicatesYall following Ys. XXXZbool isDuplicateYfalse; XXXY(YyYiY1; y < Y.LY; y++) XXX{ XXXXYY[i]YY[y]) XXXX{ XXXXXisDuplicateYtrue; XXXXXY XXXX} XXX} XXXY!isDuplicate) XXX{ XXXXY.YY[i]); XXX} XX} XXY Y; X} XYX{ XXY[] Y1Y{ 1, 2, 2, 3, 4, 4 }; XXconst Y_maxY1000000; XXvar s1YY.YNew(); XXY(YiY0; i < _max; i++) XX{Z XXXYVersion 1: benchmark distinct. XXXZvar YYTest1(Y1); XXXYY.Y() != 4) XXX{ XXXXY XXX} XX} XXs1Y; XXvar s2YY.YNew(); XXY(YiY0; i < _max; i++) XX{Z XXXYVersion 2: benchmark nested loop. XXXZvar YYTest2(Y1); XXXYY.Y() != 4) XXX{ XXXXY XXX} XX} XXs2Y; XXY(Y(s1.Y * 1000000) / XXX_max).ToY("0.00 ns")); XXY(Y(s2.Y * 1000000) / XXX_max).ToY("0.00 ns")); XXY.Y(); X} } ZResultsZ Z185.44 nsZXDistinct mY Z 51.11 nsZXNested Y-loopsZ

#:a9removes duplicate elementsIEqualityComparerbenchmarks dedupe methods