vb.net - remove dups from listview

XK8ER
XK8ER used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Meir RivkinFull stack Software Engineer

Commented:
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);
Meir RivkinFull stack Software Engineer

Commented:
to distinct listview with 12000 items in the way i've described it takes less than 200 ms.
Meir RivkinFull stack Software Engineer

Commented:
screenshot of my simple test app to distinct a 12000 items listview
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Author

Commented:
can you please provide a vb.net answer?
Meir RivkinFull stack Software Engineer

Commented:
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

Author

Commented:
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)'.
Full stack Software Engineer
Commented:
change the class declaration to this one:

Public Class DistinctListViewItemComparer
    Implements IEqualityComparer(Of ListViewItem)
    Public Overloads Function Equals(x As ListViewItem, y As ListViewItem) As Boolean Implements IEqualityComparer(Of ListViewItem).Equals
        Return x.Text = y.Text
    End Function

    Public Overloads Function GetHashCode(obj As ListViewItem) As Integer Implements IEqualityComparer(Of ListViewItem).GetHashCode
        Return obj.Text.GetHashCode()
    End Function

End Class

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial