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.
Bookmarks