@ArrAreCXBZCBC| 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).

XYZ YPYO; YPYO.LinqYSYDY; { XY$X{Z XXY{Declare an YT with some duplicated Y6sYkit. XXZY}[]Z YT1Yz{ 1, 2, 2, 3, 4, 4 };Z XXY{Invoke Distinct extension mY[. XXZvar YIYzYT1.ZDistinctZ();Z XXY{Y5 YIs. XXZY7 (YsYdYkYI) XX{Y'Y%Yd); XX} X} } Z 1 2 3 4Z YPYO; YPYO.Linq; Y YDZEqualityParityZ : IEqualityComparer<Y}> { XY=bool Equals(Ysx, Ysy) X{Z XXY{Consider all even YGs the same,YRall odd the same. XXZYJ (x % 2)Yi(y % 2); X} XY=YsGetHashCode(Ysobj) X{ XXYJ (obj % 2).GetHashCode(); X} } YDY; { XY$X{ XXZY}[]Z YT1Yz{ 9, 11, 13, 15, 2, 4, 6, 8 };Z XXY{This will rYY all except the first eventYRodd. XXZvar distinctResultYzYT1.ZDistinctZ(YwEqualityParity());Z XXY{Y5 YIs. XXZY7Z (var YIYkdistinctResult) XX{Y'Y%YI); XX} X} } Z 9 2Z YPYO; YPYO.Linq; Y Y! YDY; { XYAIEnumerable<Y}> ZTest1Z(Y}[] YT) X{Z XXY{Use distinctYlcheck Yoduplicates. XXZYJ YT.Distinct(); X} XYAIEnumerable<Y}> ZTest2Z(Y}[] YT) X{Z XXY{Use nested loopYlcheck Yoduplicates. XXZYu<Y}> YIYzYwYu<Y}>(); XXYo(YsiYz0; i < YT.LYZ; i++) XX{ZY'Y{Check YoduplicatesYkall following Y6s.Y'Zbool isDuplicateYzfalse;Y'Yo(YsyYziYy1; y < YT.LYZ; y++)Y'{Y'XYrYT[i]YiYT[y])Y'X{Y'XXisDuplicateYztrue;Y'XXYCY'X}Y'}Y'Yr!isDuplicate)Y'{Y'XYI.YnYT[i]);Y'} XX} XXYJ YI; X} XY$X{ XXY}[] YT1Yz{ 1, 2, 2, 3, 4, 4 }; XXconst Ys_maxYz1000000; XXvar s1YzY-.YcNew(); XXYo(YsiYz0; i < _max; i++) XX{ZY'Y{Version 1: benchmark distinct.Y'Zvar YIYzTest1(YT1);Y'YrYI.YW() != 4)Y'{Y'XYCY'} XX} XXs1Y1; XXvar s2YzY-.YcNew(); XXYo(YsiYz0; i < _max; i++) XX{ZY'Y{Version 2: benchmark nested loop.Y'Zvar YIYzTest2(YT1);Y'YrYI.YW() != 4)Y'{Y'XYCY'} XX} XXs2Y1; XXY%(Y.(s1.Y" * 1000000) /Y'_max).ToYM("0.00 ns")); XXY%(Y.(s2.Y" * 1000000) /Y'_max).ToYM("0.00 ns")); XXY4.Yx(); X} } ZResultsZ Z185.44 nsZXDistinct mY[ Z 51.11 nsZXNested Y|-loopsZ

:removes duplicate elementsaIEqualityComparer9benchmarks dedupe methods