Compare large arrays

J

John

I have two large arrays. Is there a rapid method to comaprs the two
arrays and creat a third array of onlt thos items that the are in the
first array but not the second array. I do it manually now (compare
each item) but it takes forever
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

I have two large arrays. Is there a rapid method to comaprs the two
arrays and creat a third array of onlt thos items that the are in the
first array but not the second array. I do it manually now (compare
each item) but it takes forever

If you sort the arrays, you can then loop through them parallelly and
easily pick out the differences.
 
M

Michel Posseth [MCP]

well i would do this with a hashtable

prepared a dirty quick example and this only takes seconds on my system


Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

' create 2 hughe arrays

Dim arrTestA(10000000) As Object

Dim arrTestB(10000000) As Object

For i As Integer = 0 To 10000000

arrTestA(i) = i

arrTestB(i) = 10500000 - i

Next

Dim arrTestC() As Object = ArrayDistinct(arrTestA, arrTestB)

MsgBox("ready")

End Sub

Public Function ArrayDistinct(ByVal ArrA() As Object, ByVal ArrB() As
Object) As Object()

Dim ht As New Hashtable(ArrA.Length)

For Each val As Object In ArrA

ht.Add(val, val)

Next

For Each val As Object In ArrB

If ht.ContainsKey(val) Then

ht.Remove(val)

End If

Next

Dim ret(ht.Count) As Object

Dim icount As Integer

For Each val As Object In ht.Values

ret(icount) = val

icount += 1

Next

Return ret

End Function





or is it not hughe enough :)
 
M

Michel Posseth [MCP]

before someone falls over the " distinct " name of the function before i
read the TS`s again and understood that he not wanted to distinct the
values
but wanted to have all values from Array A that were not in Array B

the function was mades as a distinct function ( difference is that both
values are then combined with there unique values )

Public Function ArrayDistinct(ByVal ArrA() As Object, ByVal ArrB() As
Object) As Object()

Dim ht As New Hashtable(ArrA.Length + 100) ' just a rough estimate

For Each val As Object In ArrA

ht.Add(val, val)

Next

For Each val As Object In ArrB

If Not ht.ContainsKey(val) Then

ht.Add(val, val)

End If

Next

Dim ret(ht.Count) As Object

Dim icount As Integer

For Each val As Object In ht.Values

ret(icount) = val

icount += 1

Next

Return ret

End Function


happy coding

Michel
 

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