Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Removing duplicate entries from an array in VB

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 26-07-2011
Member
 
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.

Reply With Quote
  #2  
Old 26-07-2011
Member
 
Join Date: May 2009
Posts: 523
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
Reply With Quote
  #3  
Old 26-07-2011
Member
 
Join Date: Apr 2009
Posts: 484
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
Reply With Quote
  #4  
Old 26-07-2011
Member
 
Join Date: May 2009
Posts: 532
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)
Reply With Quote
  #5  
Old 26-07-2011
Member
 
Join Date: Apr 2009
Posts: 567
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
Reply With Quote
  #6  
Old 26-07-2011
Member
 
Join Date: May 2009
Posts: 525
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
Reply With Quote
  #7  
Old 27-07-2011
Member
 
Join Date: May 2009
Posts: 620
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Removing duplicate entries from an array in VB"
Thread Thread Starter Forum Replies Last Post
Duplicate Calendar Entries ksharkman Windows Software 2 08-04-2011 12:36 AM
How to prevent duplicate database entries Zombi Software Development 3 25-09-2009 02:59 PM
How to delete duplicate entries from string array ASHER Software Development 2 02-09-2009 07:00 PM
Duplicate anniversary entries on calendar Harper Windows Software 3 01-08-2009 09:11 PM
Scavenging or something else to cleanup duplicate DNS entries? User Name Windows Server Help 4 06-10-2008 07:36 PM


All times are GMT +5.5. The time now is 08:29 AM.