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?
chadmanvbAsked:
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.

ChloesDadCommented:
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

  IF NOT FoundRecord THEN
      console.writeline(ArrB(Count1))
  END IF
Next Count1

Open in new window


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

Your issues matter to us.

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

Start your 7-day free trial
Fernando SotoRetiredCommented:
Hi chadmanvb;

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()

Open in new window


If all elements of arrB are in arrA then the result is an array with zero elements
0
chadmanvbAuthor 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
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Fernando SotoRetiredCommented:
What is the format of the strings in StringBuilder object?
0
chadmanvbAuthor Commented:
im not sure how to tell.  Heres how I build it.

 Public Sub TSRestrictedDayOld()
        'read day old file
        arrTSListOld.Clear()
        Dim strOldList() As String
        strOldList = IO.File.ReadAllLines("c:\temp\TSMembersDayOld.txt")
        For Each line In strOldList
            arrTSList.AppendLine(line)
        Next
    End Sub
0
Fernando SotoRetiredCommented:
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
ChloesDadCommented:
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)

        NewList = IO.File.ReadAllLines("c:\temp\TSMembersDayOld.txt")
        ' 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
chadmanvbAuthor Commented:
Great ChloesDad!  I will give that a shot and thanks for the tips!

FernandoSoto, the list looks like:
userid1
userid2
userid3
0
ChloesDadCommented:
Did this solve your problem?
0
chadmanvbAuthor Commented:
got both solutions working...thanks so much!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.