Link to home
Start Free TrialLog in
Avatar of XK8ER
XK8ERFlag for United States of America

asked on

vb.net - remove dups from listview

hello,
I am using this code to remove dups from listview and its working perfectly fine for small amounts like 100 or 200 items..
today I got a file that needs to be checked for dups but its 12,000 lines and it gets frozen..
how can I speed up the process and unfreeze?

    Public Sub RemoveDuplicates(ByVal lstView As ListView)
        Try
            Dim itemI, itemJ As ListViewItem
            lstView.BeginUpdate()
            For i As Integer = lstView.Items.Count - 1 To 0 Step -1
                itemI = lstView.Items(i)
                For z As Integer = i + 1 To lstView.Items.Count - 1 Step 1
                    itemJ = lstView.Items(z)
                    If itemI.Text = itemJ.Text Then
                        lstView.Items.Remove(itemJ)
                        Exit For
                    End If
                Next z
            Next (i)
            lstView.EndUpdate()
        Catch ex As Exception
        End Try
    End Sub

Open in new window

Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

first of all u should remove the dups before inserting them to the listview if that possible.

if u have list of string u can use Distinct() of linq:
var noDupsList = org_list.Distinct();

obviously u can do the same on the listview control but u need first  a custom equality comparer:

class DistinctListViewItemComparer : IEqualityComparer<ListViewItem> {

    public bool Equals(ListViewItem x, ListViewItem y) {
        return x.Text == y.Text;
    }

    public int GetHashCode(ListViewItem obj) {
        return obj.Text.GetHashCode();
    }
}

Open in new window


then u need to run this code:

//get the distinct listview items
            var items = listView1.Items.Cast<ListViewItem>().Distinct(new DistinctListViewItemComparer()).ToArray();

//clear the listview
            listView1.Items.Clear();

//add the distinct listview items to the listview
            listView1.Items.AddRange(items);
to distinct listview with 12000 items in the way i've described it takes less than 200 ms.
Avatar of XK8ER

ASKER

can you please provide a vb.net answer?
Class DistinctListViewItemComparer
	Implements IEqualityComparer(Of ListViewItem)

	Public Overloads Function Equals(x As ListViewItem, y As ListViewItem) As Boolean
		Return x.Text = y.Text
	End Function

	Public Overloads Function GetHashCode(obj As ListViewItem) As Integer
		Return obj.Text.GetHashCode()
	End Function
End Class

Open in new window


usage:
'get the distinct listview items
Dim items = listView1.Items.Cast(Of ListViewItem)().Distinct(New DistinctListViewItemComparer()).ToArray()

'clear the listview
listView1.Items.Clear()

'add the distinct listview items to the listview
listView1.Items.AddRange(items)

Open in new window

Avatar of XK8ER

ASKER

this line
>>Implements IEqualityComparer(Of ListViewItem)

Error      1      Class 'DistinctListViewItemComparer' must implement 'Function Equals(x As Windows.Forms.ListViewItem, y As Windows.Forms.ListViewItem) As Boolean' for interface 'System.Collections.Generic.IEqualityComparer(Of System.Windows.Forms.ListViewItem)'.
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial