Skip to content

Instantly share code, notes, and snippets.

@jamescurran
Created February 15, 2025 16:05
Show Gist options
  • Select an option

  • Save jamescurran/0e0d9781d6e1ea761b4495c6008f0714 to your computer and use it in GitHub Desktop.

Select an option

Save jamescurran/0e0d9781d6e1ea761b4495c6008f0714 to your computer and use it in GitHub Desktop.
Daily Challenge #JS-102: Calculate Unique Elements in a Square Matrix (In C#)
/*
Calculate Unique Elements in a Square Matrix
Given a square matrix, write a function that computes the number of unique elements in the matrix. A square matrix is a two-dimensional array where the number of rows is equal to the number of columns.
Example:
Input: [[2, 3, 2], [9, 3, 1], [1, 2, 5]]
Output: 5
*/
int countUniqueElements(int[][] matrix) => matrix.SelectMany(m => m).Distinct().Count();
void Main()
{
int[][] matrix = new int[][] { new int[] { 2, 3, 2 }, new int[] { 9, 3,1 }, new int[] { 1, 2, 5 }};
countUniqueElements(matrix).Dump();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment