+ Reply to Thread
Results 1 to 4 of 4

Comparing Arrays: What are tradeoffs b/w using a Dictionary vs a nested For-Each loops?

  1. #1
    Forum Contributor
    Join Date
    12-05-2015
    Location
    Akron, OH
    MS-Off Ver
    15.0
    Posts
    424

    Comparing Arrays: What are tradeoffs b/w using a Dictionary vs a nested For-Each loops?

    I have two arrays, say Arr1 & Arr2. Arr1 will have unique elements (compared with other elements in Arr1 but not necessarily Arr2). Similarly for Arr2 (unique elements...). I want to compare the contents between Arr1 & Arr2 and if there are values in Arr2 which are not in Arr1, I want to add those to Arr1.

    I want to know what the trade-offs are between using the following two methods for doing that, which I'll describe below. If there is a better way, please comment.

    Method 1: Using a dictionary
    Step 1: set all elements of Arr1 to the keys of a dictionary with arbitrary/empty items.
    Step 2: loop through Arr2 with a For-Each loop and use the dictionary.Exists method to determine if there is an element in Arr2 which does not exist in Arr1.
    Step 3: add keys in the dictionary to Arr1
    Or...
    Method 1:
    Step 1: Loop over All elements in Arr1 & Arr2 with embedded For-Each loops
    Step 2: Compare each element
    Step 3: ReDim Preserve Arr1(UBound(Arr1) + 1) and Arr1(UBound(Arr1)) = New element in Arr2 which is not in Arr1

    Are there substantially more memory resources used for a dictionary object?
    Does the speed using the .Exists method overtake the nested For-Each loop? For small arrays? For large arrays?

    I appreciate anyone's expertise they'd like to share.
    <---If my answer helped, please click *

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Comparing Arrays: What are tradeoffs b/w using a Dictionary vs a nested For-Each loops

    Quote Originally Posted by joe31623 View Post
    I have two arrays, say Arr1 & Arr2. Arr1 will have unique elements (compared with other elements in Arr1 but not necessarily Arr2). Similarly for Arr2 (unique elements...). I want to compare the contents between Arr1 & Arr2 and if there are values in Arr2 which are not in Arr1, I want to add those to Arr1.

    I want to know what the trade-offs are between using the following two methods for doing that, which I'll describe below. If there is a better way, please comment.

    Method 1: Using a dictionary
    Step 1: set all elements of Arr1 to the keys of a dictionary with arbitrary/empty items.
    Step 2: loop through Arr2 with a For-Each loop and use the dictionary.Exists method to determine if there is an element in Arr2 which does not exist in Arr1.
    Step 3: add keys in the dictionary to Arr1
    Or...
    Method 1:
    Step 1: Loop over All elements in Arr1 & Arr2 with embedded For-Each loops
    Step 2: Compare each element
    Step 3: ReDim Preserve Arr1(UBound(Arr1) + 1) and Arr1(UBound(Arr1)) = New element in Arr2 which is not in Arr1

    Are there substantially more memory resources used for a dictionary object?
    Does the speed using the .Exists method overtake the nested For-Each loop? For small arrays? For large arrays?

    I appreciate anyone's expertise they'd like to share.
    The memory resources for Dictionaries are negligible. Not much more than having an array of results.

    The dictionary method would be significantly faster for large arrays. Less noticeable for small arrays, but still faster.

    If you use the Dictionary method, you wouldn't necessarily have to use .Exists to test. Just add both arrays to the dictionary. Unique items will be added and duplicates will be unchanged for the existing item (in other words, ignored).

    Example:
    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    12-05-2015
    Location
    Akron, OH
    MS-Off Ver
    15.0
    Posts
    424

    Re: Comparing Arrays: What are tradeoffs b/w using a Dictionary vs a nested For-Each loops

    That clears up some misc. questions I had about some of the more advanced users' solutions I see and will help me code better. Thank you for the detailed explanation!

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Comparing Arrays: What are tradeoffs b/w using a Dictionary vs a nested For-Each loops

    You're welcome. Thanks for the feedback.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Trying to search for list of strings using Arrays,Scripting Dictionary or FileSystemObject
    By downtown1933 in forum Excel Programming / VBA / Macros
    Replies: 42
    Last Post: 10-01-2013, 04:38 AM
  2. [SOLVED] Loops and arrays
    By LadyB in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-12-2013, 04:28 PM
  3. Syntax for arrays and loops in VBA
    By DataMole in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-23-2012, 12:43 PM
  4. Need help with loops and arrays in VBA Macro
    By lealea1982 in forum Excel Programming / VBA / Macros
    Replies: 23
    Last Post: 08-26-2011, 08:51 AM
  5. VBA Loops + Arrays
    By Kengh in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-26-2010, 07:41 PM
  6. Loops and Arrays
    By v2jtb in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-29-2010, 12:09 PM
  7. [SOLVED] slow for loops ... better way using arrays or something?
    By wdeleo in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-28-2006, 10:35 AM
  8. [SOLVED] VBA excel using arrays and loops
    By vincentwongau@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-06-2006, 08:55 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1