Copy unique values to new array

  • Thread starter Thread starter Joep
  • Start date Start date
J

Joep

Hi,

In VB.net I have an array which contains different values. For example:
array(0) = 2
array(1) = 2
array(2) = 2
array(3) = 3
array(4) = 3
array(5) = 6
array(6) = 6
array(7) = 4
array(8) = 4
array(9) = 4
I'd like to copy the unique values in this array to a new array, which
contains only these unique values. In this example arraySecond would be:
arraySecond(0) = 2
arraySecond(1) = 3
arraySecond(2) = 6
arraySecond(3) = 4

Can anyone help me with the code for creating this second array based on the
first? I cannot find a way to accomplish this.

Thanks in advance,
Joep
 
Joep,

mmm... that's pure logic stuff. I have an example below, but I would think of using an ArrayList instead of a regular array. Good luck!
//
Function GetArray(ByVal array() As Integer) As Integer()

Dim x, y, n, sa() As Integer

y = -1

n = 0

array.Sort(array)

For Each x In array

If x <> y Then

ReDim Preserve sa(n)

sa(n) = x

y = x

n += 1

End If

Next

Return sa

End Function

//

Telmo Sampaio
 

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