# compare 2 arrays

I have 2 arrays that contain a bunch of user ids called arrA and arrB.  How can I find the user id's in arrB that do not exsist in arrA with visual basic .net?
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
A simple dual loop will do the job

``````dim Count1 as integer
Dim Count2 as integer
Dim FoundRecord as Boolean = False

For Count1=0 to ArrB.Length - 1
FoundRecord = False
For Count2 = 0 to ArrA.Length - 1
IF ArrA(Count2)= ArrB(Count1) THEN
FoundRecord = True
END IF
NEXt Count2

console.writeline(ArrB(Count1))
END IF
Next Count1
``````

The debug window will show all of the user IDs in Arrb that are not in ArrA
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

RetiredCommented:

Here is a simple Linq query that will do what you need.

``````' Sample Data
Dim arrA() As Integer = { 1,40,25,78,93,10,66}
Dim arrB() As Integer = { 93,66,25,10,1,22,78,40,101,37}

' Given arrB what elements are NOT in arrA
' donotExistInA is an array of integers with the ID's
Dim donotExistInA = arrB.Except(arrA).ToArray()
``````

If all elements of arrB are in arrA then the result is an array with zero elements
0
Author Commented:
Sorry for the long delay, but they both look like great solutions.  I did mention an array, but really it's a stringbuilder I created.

Dim arrTSList As New StringBuilder
Dim arrTSListOld As New StringBuilder

I'm not sure how to convert between the stringbuild and array to get this working.  Could I do the same thing with the stringbuilder?
0
RetiredCommented:
What is the format of the strings in StringBuilder object?
0
Author Commented:
im not sure how to tell.  Heres how I build it.

Public Sub TSRestrictedDayOld()
arrTSListOld.Clear()
Dim strOldList() As String
For Each line In strOldList
arrTSList.AppendLine(line)
Next
End Sub
0
RetiredCommented:
In order to be able to help you I need to know what the data looks like in the file "c:\temp\TSMembersDayOld.txt". Can you post the file or attach the file here?
0
Commented:
The problem with using a string builder is that you then have to delimit the string to get back to an array for at least one of the string builders. And you definitely should not be suffixing a stringbuilder with arr as everyone will assume that its an array, which it isn't. In fact use of Hungarian notation is now considered bad practice.

You already have the data as a string array, so you can work with that using FernandoSoto's method as its just one line. :)

' copy the last newlist to OldList for comparing
Array.Copy(NewList, OldList, NewList.Length)

' here NewList is already a string array, so we dont need to do anything with it

' donotExistInOld is an array of strings
Dim donotExistInOld = NewList.Except(OldList).ToArray()
0
Author Commented:
Great ChloesDad!  I will give that a shot and thanks for the tips!

FernandoSoto, the list looks like:
userid1
userid2
userid3
0
Commented: