I need to compare 2 lists and remove elements that appear in the first array from the second one efficiently. My solutions below:
Homebrew algorithm:
#function accepts 2 arrays. Compares elements from 1st array to 2nd to see if they exist in 2nd,
#and removes them from 2nd array.
def compareRemove(self, array1 = [], array2 = []):
#starting iteration values
i = 0
j = 0
array1_length = len(array1)
array2_length = len(array2)
while i < array1_length:
while j < array2_length:
#compare element from first array to second array, remove element if same,
#and update second array length
if array1[i] == array2[j]:
del array2[j]
array2_length = len(array2)
#otherwise move j forward
else:
j = j + 1
#when 2nd while loop is done move i forward and reset j to 0
i = i + 1
j = 0
return array2
suggested from research on SO:
updated_file_strings = [x for x in array2 if not x in array1]
Both are similarly fast, however as my array lenghts increase both will slow down. Are there any methods I can utilise that will perform well even if array lengths increase? Order doesnt matter.
EXAMPLE:
array1 = [1, 2, 3]
array2 = [1, 2, 3, 5, 5, 5]
result_array = [5, 5, 5]
duplicates within the second array should NOT be removed.