I have a structure
Public Structure MyRecords
Dim record_name As String
Dim record_surname As String
End Structure
and an array of structures
Dim Records_arr(100000) AS MyRecords
The array is full of data, but i want the duplicate MyRecords to be removed.
Duplicate means:
Records_arr(3).record_name = "John"
Records_arr(3).record_surname = "Doe"
Records_arr(99).record_name = "John"
Records_arr(99).record_surname = "Doe"
In this example i want Records_arr(3) or Records_arr(99) to be removed, or just empty the values of one of them.
I have already implemented a 2-pass scan top-to-bottom and then bottom-to-top that wipes out duplicates, but it's very very slow. There's got to be a better way?
Implement IEquatable<T> on your structure and then use LINQ's Distinct() function to get your goals.
Here's a sample implementation:
Public Structure MyRecords
Implements IEquatable(Of MyRecords)
Dim record_name As String
Dim record_surname As String
Public Function Equals1(other As MyRecords) As Boolean Implements IEquatable(Of MyRecords).Equals
Return record_name.Equals(other.record_name) AndAlso record_surname.Equals(other.record_surname)
End Function
End Structure
Now you can call Records_arr.Distinct() and it will return unique records of your array.
Further Explanation
In response to your comment, here's what's going on. Since you're using a Structure and not a Class, MyRecords already inherits ValueType.Equals() method. For value types that do not contain any reference type members, the Equals() method performs byte-by-byte comparison of the two objects in memory. On the other hand, if it does contain reference type members (which is your case), it uses Reflection to compare the corresponding fields of the two instance. You have two String fields in your structure, which are reference types and are therefore compared through Reflection. (Important to note that String class, although a reference type, overrides Equals() to provide value comparison instead of the default reference comparison). Thus even if you do not implement IEquatable or IComparable on your class, Distinct() will work fine using the default ValueType.Equals() function.
Be advised however that MSDN asks you to provide Equals() implementation for this kind of structures. Here's the relevant excerpt:
Particularly if your value type contains fields that are reference types, you should override the Equals(Object) method. This can improve performance and enable you to more closely represent the meaning of equality for the type.
You can read more about it in this MSDN article.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With