OneBite.Dev - Coding blog in a bite size

find the unique elements in two arrays in python

Code snippet on how to find the unique elements in two arrays in python

  def FindUnique(arr1, arr2): 
    final_list =[] 
    for num in arr1: 
        if num not in arr2: 
            final_list.append(num) 
  
    for num in arr2: 
        if num not in arr1: 
            final_list.append(num) 
            
    return final_list 

This code finds the unique elements in two arrays. “arr1” and “arr2” are two arrays. The code first creates an empty array called “final_list”, which is where the unique elements will be stored. It then iterates through the elements of ‘arr1’. For each element in ‘arr1’, it checks to see if that element is contained in ‘arr2’. If not, it adds it to ‘final_list’. The code then does the same for ‘arr2’. It checks to see if each element of ‘arr2’ is contained in ‘arr1’. If not, that element is added to ‘final_list’. Finally, the function returns ‘final_list’, which contains all of the unique elements in both of the original arrays.

python