how to get an array without duplicated values?

E

Eric

Hi,

i have an array like this:
x(1)=20
x(2)=40

and another:
y(1)=10
y(2)=20
y(3)=30
y(4)=40
y(5)=50

i want to put the values of array y() into a listbox minus the values of
array x():
so in this case, only y(1), y(3) and y(5) are allowed in the listbox.

I can do it but still with duplicated values, what i don't want.
I compare each y() with each x(): if not equals => into listbox, else no.
But then i get duplicated values.

Thanks for help
Eric
 
A

Armin Zingler

Eric said:
Hi,

i have an array like this:
x(1)=20
x(2)=40

and another:
y(1)=10
y(2)=20
y(3)=30
y(4)=40
y(5)=50

i want to put the values of array y() into a listbox minus the values
of array x():
so in this case, only y(1), y(3) and y(5) are allowed in the listbox.

I can do it but still with duplicated values, what i don't want.
I compare each y() with each x(): if not equals => into listbox, else
no. But then i get duplicated values.


4 versions: (I'd use the last one)
(untested ;-) )

For i = 1 To 5
Dim found As Boolean = False
For j = 1 To 2
If y(i) = x(j) Then
found = True
Exit For
End If
Next

If Not found Then
lst.Items.Add(y(i))
End If
Next

For i = 1 To 5
If Not x.Contains(y(i)) Then
lst.Items.Add(y(i))
End If
Next

For Each item In y.Where(Function(value As Integer) Not
x.Contains(value))
lst.Items.Add(item)
Next

For Each value In y.Except(x)
lst.Items.Add(value)
Next


Armin
 
E

Eric

Thanks

Armin Zingler said:
4 versions: (I'd use the last one)
(untested ;-) )

For i = 1 To 5
Dim found As Boolean = False
For j = 1 To 2
If y(i) = x(j) Then
found = True
Exit For
End If
Next

If Not found Then
lst.Items.Add(y(i))
End If
Next

For i = 1 To 5
If Not x.Contains(y(i)) Then
lst.Items.Add(y(i))
End If
Next

For Each item In y.Where(Function(value As Integer) Not
x.Contains(value))
lst.Items.Add(item)
Next

For Each value In y.Except(x)
lst.Items.Add(value)
Next


Armin
 

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

Top