Delete duplicate rows from two dimentsional array

Mitya

Let's say I have two dimensional array that represents simple matrix

int[,] matrix= new int[,] { { 1, 2 }, { 3, 4 }, { 1, 2 }, { 7, 8 } };

It looks like that

1 2
3 4
1 2
7 8

Is there any way to delete duplicate rows using LINQ and make array to look like this?

1 2
3 4
7 8
Cyril Gandon

This is not really Linq, but you can define some helper method as if they were Linq methods.

The simpler algorithm should be:

  1. Convert to a list of list
  2. Apply a distinct with a custom comparer
  3. Rebuild another array

This looks like this:

public static class MyExtensions
{
    public static IEnumerable<List<T>> ToEnumerableOfEnumerable<T>(this T[,] array)
    {
        int rowCount = array.GetLength(0);
        int columnCount = array.GetLength(1);

        for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
        {
            var row = new List<T>();
            for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)
            {
                row.Add(array[rowIndex, columnIndex]);
            }
            yield return row;
        }
    }
    public static T[,] ToTwoDimensionalArray<T>(this List<List<T>> tuples)
    {
        var list = tuples.ToList();
        T[,] array = null;
        for (int rowIndex = 0; rowIndex < list.Count; rowIndex++)
        {
            var row = list[rowIndex];
            if (array == null)
            {
                array = new T[list.Count, row.Count];
            }
            for (int columnIndex = 0; columnIndex < row.Count; columnIndex++)
            {
                array[rowIndex, columnIndex] = row[columnIndex];
            }
        }
        return array;
    }
}

The custom List comparer (copied from a Jon Skeet's answer):

public class ListEqualityComparer<T> : IEqualityComparer<List<T>>
{
    public bool Equals(List<T> x, List<T> y)
    {
        return x.SequenceEqual(y);
    }

    public int GetHashCode(List<T> obj)
    {
        int hash = 19;
        foreach (var o in obj)
        {
            hash = hash * 31 + o.GetHashCode();
        }
        return hash;
    }
}

The usage :

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        var array = new[,] { { 1, 2 }, { 3, 4 }, { 1, 2 }, { 7, 8 } };
        array = array.ToEnumerableOfEnumerable()
                     .Distinct(new ListEqualityComparer<int>())
                     .ToList()
                     .ToTwoDimensionalArray();
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Delete duplicate rows from Multidimensional Object array [,]?

From Dev

Delete duplicate rows from dataset

From Dev

Delete duplicate rows in two columns simultaneously

From Dev

Delete duplicate rows from table with no unique key

From Dev

Delete duplicate rows from a BigQuery table

From Dev

How to delete duplicate rows from mysql

From Dev

mysql delete duplicate rows from table

From Dev

Repetitive(duplicate) Element Delete from an Int Array

From Dev

Delete the duplicate words from array of strings in MySQL

From Dev

Delete duplicate rows in calc?

From Dev

Delete duplicate rows in mysql

From Dev

delete rows if duplicate exist

From Dev

Delete duplicate rows in a table

From Dev

Delete rows from two tables related

From Dev

GQL Query: Delete duplicate rows in GQL from the datastore viewer

From Dev

how could i delete rows with repeating/duplicate index from dataframe

From Dev

Unix Delete Duplicate rows from csv based on 2 columns

From Dev

Delete rows at select indexes from a numpy array

From Dev

Delete duplicate array element

From Dev

Delete duplicate elements in an array

From Dev

MySQL delete duplicate rows in table

From Dev

Delete Rows With Duplicate Data VBA

From Dev

Delete only consecutive duplicate rows

From Dev

Find and Delete Duplicate rows in MySQL

From Dev

How to delete duplicate rows in Oracle

From Dev

Find and Delete Duplicate rows in MySQL

From Dev

Delete duplicate rows with same text

From Dev

delete duplicate rows SQL Server

From Dev

Select two random rows from numpy array