# question

## Creating an IEqualityComparer for Arrays and Distinct

I have an array of arrays of integer (a jagged array, Integer()()) from which I want to remove duplicates. For example, if I have the following:

`````` Dim testvalues As Integer()() = {
({2, 4, 3, 1, 5}),
({1, 3, 5, 2, 4}),
({5, 4, 3, 2, 1}),
({1, 2, 3, 4, 5}),
({2, 4, 1, 3, 5}),
({1, 2, 3, 4, 5}),
({5, 4, 3, 2, 1}),
({1, 2, 3, 4, 5})}
``````

I want to use the Distinct() method to get the following:

`````` Dim distincttestvalues As Integer()() = {
({2, 4, 3, 1, 5}),
({1, 3, 5, 2, 4}),
({5, 4, 3, 2, 1}),
({1, 2, 3, 4, 5}),
({2, 4, 1, 3, 5})}
``````

I have been unable to find an existing way to do this, so I figured the best solution would be to create an IEqualityComparer (which I have never done before) that uses the SequenceEqual method. Is this the best way to remove duplicate arrays, or do I need to resort to manually finding them using loops & if statements? Thanks.

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I think I managed to find an answer that I like, I created the following class by inheriting from EqualityComparer:

`````` Public Class ArrayComparer(Of T) : Inherits EqualityComparer(Of T())
Public Overrides Function Equals(x As T(), y As T()) As Boolean
Return x.SequenceEqual(y)
End Function
Public Overrides Function GetHashCode(obj As T()) As Integer
Return obj.Length.GetHashCode()
End Function
End Class
``````

And then I just used .Distinct(New ArrayComparer(Of Integer)()) in my code. I knew there had to be a simple way to do it, I think this was just one of those first time things (and I have always been somewhat confused by the GetHashCode method). But this seems to work well enough (at least for my scenario).

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello,

Welcome to Microsoft Q&A!

Q&A currently is under a public preview, All the supported topics are on the right top. Your issue is not supported right now, you can ask it in this forum.

Thanks.

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, you can write your own Extension like in this console demo:

`````` Imports System.Runtime.CompilerServices

Module Module48

Sub Main()
Try
Call (New Demo).Execute()
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
Console.WriteLine("Continue enter key")
End Sub

Friend Class Demo
Friend Sub Execute()
Dim testvalues As Integer()() = {
({2, 4, 3, 1, 5}),
({1, 3, 5, 2, 4}),
({5, 4, 3, 2, 1}),
({1, 2, 3, 4, 5}),
({2, 4, 1, 3, 5}),
({1, 2, 3, 4, 5}),
({5, 4, 3, 2, 1}),
({1, 2, 3, 4, 5})}

Dim distincttestvalues = testvalues.DistinctTest

For i = 0 To distincttestvalues.GetUpperBound(0)
For k = 0 To distincttestvalues(i).GetUpperBound(0)
Console.Write(\$" {distincttestvalues(i)(k)}")
Next
Console.WriteLine()
Next

End Sub
End Class

End Module

Public Module ArrayExtensions

<Extension()>
Public Function DistinctTest(inp As Integer()()) As Integer()()
Dim cacheList As New List(Of Integer())
For i = 0 To inp.GetUpperBound(0)
Dim sw As Boolean = False
For k = 0 To cacheList.Count - 1
If inp(i).SequenceEqual(cacheList(k)) Then
sw = True
Exit For
End If
Next
If Not sw Then cacheList.Add(inp(i))
Next
Dim out(cacheList.Count - 1)() As Integer
For i = 0 To out.GetUpperBound(0)
out(i) = cacheList(i)
Next
Return out
End Function

End Module
``````

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.