Removing repeated terms in an array - please help...

  • Thread starter Thread starter almurph
  • Start date Start date
A

almurph

Folks,

I have an array of chars nad would like to remove any repeating
terms. Any ideas as how to do this?

Thanks,
Al.
 
This is data compression, if the array of char is fixed and not dynamic then
you could send it into a For Each Char in CharArray loop?

Create a StringBuilder, because strings in vb.net cannot be changed, its in
the imports system.text area?

first char = the variable that holds the 'compare to' char, compare to
nothing, its different, so append it to the stringbuilder instance

the Next char, compare to the 'compare to' char, if different, append it to
the stringbuilder instance, else next
 
Try this code. Just pass the original array to this function and it
returns an array with the duplicates removed.

Private Function RemoveDupes(ByVal myArr As Array) As Array

Dim i As Integer = 0
Dim j As Integer = 0
' Creates and initializes a new Array
Dim myArr2 As [String]()
Try
ReDim myArr2(myArr.Length)

' Sorts the entire Array using the default comparer.
Array.Sort(myArr)

myArr2(0) = CType(myArr.GetValue(0), String)
For i = 1 To myArr.Length - 1
If (myArr2(j) <> CType(myArr.GetValue(i), String)) Then
j += 1
myArr2(j) = CType(myArr.GetValue(i), String)
End If
Next i
ReDim Preserve myArr2(j)

Return myArr2
Catch ex As Exception
MsgBox(ex, MsgBoxStyle.Critical)
End Try

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top