Results 1 to 7 of 7

Thread: Removing duplicate entries from an array in VB

  1. #1
    Join Date
    Jun 2011
    Posts
    98

    Removing duplicate entries from an array in VB

    Please help me. I have a record of many entries of string which is stored in an array. My query is that I want to remove all the duplicate entries. I have tried but could not get it. Can any one of you experts help me with the code, so that I can implement it soon in my VB code? All you’re your replies will be appreciated.

  2. #2
    Join Date
    May 2009
    Posts
    527

    Re: Removing duplicate entries from an array in VB

    The below code I have edited for you. I have not personally tested but it should work for you. I tried my best to make the code easier.
    Code:
    dim temparray(-1) as string
    dim i as integer = ubound(myarray)
    dim j as integer
    dim currentstring as string
    myarray.sort
    for j =0 to i-1
    if currentstring <> myarray(j) then
    temparray.append myarray(j)
    currentstring=myarray(j)
    //keep path of the current string to test for duplicates.
    Code:
    end if
    next j

  3. #3
    Join Date
    Apr 2009
    Posts
    488

    Re: Removing duplicate entries from an array in VB

    I would like to add more code. The easiest way is to sort the entries and then compare it.
    // Now replace your array with the temp that I have built for you.

    Code:
    redim myarray(-1)
    i=ubound(temparray)
    for j=0 to i-1
    myarray.append temparray(j)
    next j

  4. #4
    Join Date
    May 2009
    Posts
    539

    Re: Removing duplicate entries from an array in VB

    As far as I am concerned you have to add all the strings of the array in the dictionary. After finishing that, the dictionary will contain only values that are unique and it can be treated like an array.
    Code:
    Code:
    dim mydictionary as new dictionary
    for x = 0 to ubound(myarray)
       mydictionary.value (myarray(x)) = 1
    next
    //now all the unique values can be used like using keys() like an array

    Code:
    s  = mydictionary.keys(1)

  5. #5
    Join Date
    Apr 2009
    Posts
    569

    Re: Removing duplicate entries from an array in VB

    The below code that I have edited for you is a straight forward process. After having a glance over the code you can easily make out what’s going on as it is easy to understand. If any of the code does not work for you than you can use it.

    Code:
    Sub RemoveDuplicates ( MyArray () As String )
      Dim I, UpperBound1 As Integer
      MyArray.Sort
      UpperBound1 = Ubound ( MyArray ) 
      For I = UpperBound1 DownTo 1
        If MyArray ( I ) = MyArray ( I - 1 ) Then
          MyArray.Remove I
        End If 
      Next I
    End Sub

  6. #6
    Join Date
    May 2009
    Posts
    529

    Re: Removing duplicate entries from an array in VB

    Another alternative you can try in your actual source code. You have to add two things that I have mentioned below:
    Code:
    Function NonDuplicateArray(List() As String) As String()
      Dim NewList() As String
      Dim Count As Integer = List.Ubound
    
      For x As Integer = 0 To Count
        If (NewList.IndexOf(List(x)) = -1) Then NewList.Append List(x)
      End For
    End Function
    The below code will overwrite the array and preserve the original array for you:
    Code:
    Dim NewArray() As String
    
    OriginalArray = NonDuplicateArray(OriginalArray) // Overwrites array
    NewArray = NonDuplicateArray(OriginalArray) // Preserves original

  7. #7
    Join Date
    May 2009
    Posts
    637

    Re: Removing duplicate entries from an array in VB

    If you ask me the set back with the dictionary method is that they are not case sensitive, also while comparing stings. For example if you have “World” or “world” than they will look the same. For removing the case sensitive’s try the following:

    Code:
    Function RemoveDuplicates(s() as String, isCaseSensitive as Boolean = true) As String()
      dim returnString() as String
      
      //We know we want the first one, so append it to returnString
      returnString.Append s(0)
      
      //Loop through all the values in the input array
      //and compare them to our returnString
      //If it exists, don't append it
      for i as Integer = 1 to s.Ubound
        dim exists as Boolean = false
        
        for j as Integer = 0 to returnString.Ubound
          if isCaseSensitive then
            //If we want the comparison to be case sensitive, use StrComp
            //("Hello"="hello" returns nonzero)
            if StrComp(s(i), returnString(j), 1) = 0 then
              exists = true
              exit for //we don't need to keep looping through returnString, we already found a match
            end if
          else
            //Comparing text with = is not case sensitive (so "Hello"="hello" returns true)
            if s(i) = returnString(j) then
              exists = true
              exit for //we don't need to keep looping through returnString, we already found a match
            end if
          end if
        next
        
        //If we didn't find a match, append the original value to returnString
        if exists = false then
          returnString.Append s(i)
        end if
      next
      
      Return returnString()
      
    End Function

Similar Threads

  1. Duplicate Calendar Entries
    By ksharkman in forum Windows Software
    Replies: 2
    Last Post: 08-04-2011, 12:36 AM
  2. How to prevent duplicate database entries
    By Zombi in forum Software Development
    Replies: 3
    Last Post: 25-09-2009, 02:59 PM
  3. How to delete duplicate entries from string array
    By ASHER in forum Software Development
    Replies: 2
    Last Post: 02-09-2009, 07:00 PM
  4. Duplicate anniversary entries on calendar
    By Harper in forum Windows Software
    Replies: 3
    Last Post: 01-08-2009, 09:11 PM
  5. Scavenging or something else to cleanup duplicate DNS entries?
    By User Name in forum Windows Server Help
    Replies: 4
    Last Post: 06-10-2008, 07:36 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,297,898.25727 seconds with 17 queries