Removing Duplicates in a DropDownList

In a recent project enhancing some functionality to an existing .Net v2.x web application I had the need to remove duplicate entries in a DropDownList.  As with most challenges I haven’ had the pleasure of resolving before, I took it to Google.  Of course I got a plethora of suggestions to eliminate the duplicates in the data before binding or query the database with DISTINCT.

But I was dealing with an existing application, that stored the query results in an ArrayList of data objects.  Also the results included location data, such as City, State, Market and Region, that I was providing drop down filters on, so eliminating the duplicates in the query was out of the question and I did not want to make another query to the database.

Eventually I found a solution in this post and modified it to be more generic as seen here:

 

Code Snippet
  1. Public Shared Function DDL_RemoveDuplicateItems(ByVal ddl As DropDownList) As DropDownList
  2.     For i As Integer = 0 To ddl.Items.Count – 1
  3.         ddl.SelectedIndex = i
  4.         Dim str As String = ddl.SelectedItem.ToString()
  5.         For counter As Integer = i + 1 To ddl.Items.Count – 1
  6.             ddl.SelectedIndex = counter
  7.             Dim compareStr As String = ddl.SelectedItem.ToString()
  8.             If str = compareStr Then
  9.                 ddl.Items.RemoveAt(counter)
  10.                 counter = counter – 1
  11.             End If
  12.         Next
  13.     Next
  14.     Return ddl
  15. End Function

 

 

But I quickly found that if all rows were from the same State or Region, all entries were duplicates and this function would delete all entries, but I wanted to leave at least one. Also, it did not properly handle deleting the last entry in the DDL, so I modified it and it works quite well.

 

Code Snippet
  1. Public Shared Function DDL_RemoveDuplicateItems(ByVal ddl As DropDownList) As DropDownList
  2.     For i As Integer = 0 To ddl.Items.Count – 1
  3.         ddl.SelectedIndex = i
  4.         Dim str As String = ddl.SelectedItem.ToString()
  5.         For counter As Integer = i + 1 To ddl.Items.Count – 1
  6.             ddl.SelectedIndex = counter
  7.             Dim compareStr As String = ddl.SelectedItem.ToString()
  8.             If str = compareStr Then
  9.                 Try
  10.                     If ddl.Items.Count = 1 Then
  11.                         ‘ There is only ONE item in the list, it is not duplicate, do not remove it
  12.                         Return ddl
  13.                     ElseIf counter = ddl.Items.Count – 1 Then
  14.                         ‘ Removing last item in the list.
  15.                         ddl.Items.RemoveAt(counter)
  16.                         Exit For
  17.                     Else
  18.                         ‘ Remove item and keep going
  19.                         ddl.Items.RemoveAt(counter)
  20.                         counter = counter – 1
  21.                     End If
  22.                 Catch ex As Exception
  23.                     Dim err As String = ex.Message
  24.                 End Try
  25.             End If
  26.         Next
  27.     Next
  28.     Return ddl
  29. End Function

I realize that this functionality is not needed often but… I would think by v2.x and newer releases a RemoveDuplicates function would be provided.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.