CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2001
    Location
    Sri Lanka
    Posts
    272

    Comparing two Arrays

    There are 2 arrays (Say A & B). Both of them having some similar elements. I need to put similar elements of A & B in to another array(say C) and the Elements which are in A but not not in B in to another array (say D).
    eg.
    Elements in A: a1,a2,p,q,
    Elements in B: b1,b2,p,q
    After process
    Elements in C: p,q
    Elements in D: a1,a2
    What is the most efficient way to handle this?
    Srinika

    If u don't know how to Rate an answer, then Rate my answer to learn, If u know, then practice it

  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Comparing two Arrays


    Dim T as integer, intC as integer, intD as integer

    for t=0 to ubound(arrayA)

    if arrayA(t) = arrayB(t) then
    intC = intC + 1
    Redim Preserve arrayC(intC)
    arrayC(intC-1) = arrayA(t)
    else
    intD = intD + 1
    Redim Preserve arrayD(intD)
    arrayC(intD-1) = arrayA(t)
    end if

    next t




    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Comparing two Arrays

    You're looking only in same position of both arrays...

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  4. #4
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Comparing two Arrays

    Ok wiseass (kidding)

    private Sub Command1_Click()

    Dim t as Variant, r as Variant
    Dim colC as new Collection, colD as new Collection

    Dim ArrayA, ArrayB
    Dim ArrayC(), ArrayD()

    ArrayA = Array("1", "2", "6", "7")
    ArrayB = Array("3", "4", "6", "7")

    on error resume next
    ' check to see which are in A and B
    for Each t In ArrayA
    for Each r In ArrayB
    If t = r then
    colC.Add t, CStr(t)
    End If
    next r
    next t

    ' check to see which aren't in A and B
    for Each t In ArrayA
    r = null
    r = colC(CStr(t))
    If IsNull(r) then
    colD.Add t
    End If
    next t

    ' put them in the arrays
    ReDim ArrayC(colC.Count)
    ReDim ArrayD(colD.Count)

    r = 0
    for Each t In colC
    ArrayC(r) = t
    r = r + 1
    next t

    r = 0
    for Each t In colD
    ArrayD(r) = t
    r = r + 1
    next t

    End Sub



    Happy now? It even prevents doubles in the arrays

    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Comparing two Arrays


    Trying to give you an excelent, codeguru replied to me:

    "We encountered a problem!

    The server encountered an error and cannot complete your request.
    The error reported was:

    You have already reached the limit of 5 today.
    This restriction has been imposed to prevent misuse of the system.
    Note: to the program, the time frame for a day is based on California time."
    ...

    Hope someone here will rate your code!
    ps: Chris rated me today!





    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured