DEV Community

Andre Carstens
Andre Carstens

Posted on

C# arrays using Distinct

int[] nums = new int[] { 1, 4, 5, 2, 3, 5, 6 };
//unique values
var unique = nums.Distinct();
Enter fullscreen mode Exit fullscreen mode

Distinct() will shorten the integer array by removing any duplicate values and returning a shortened array.

Can you select the values from an integer array that you want to apply Distinct to?

var selectedDistinct = nums.Where(x => x >= 3).Select(x => x).Distinct();

Enter fullscreen mode Exit fullscreen mode

The code above will shorten nums to only values that are greater or equal to 3 and then apply Distinct to that shortened result.

Top comments (0)