I have two arrays that I want to combine and then eliminate both values if duplicates appear. How can I achieve this?
arr1 = [1,2,3,4,5,6,7]
arr2 = [3,5,6,7]
I want to be left with
arr3 = [1,2,4]
Thanks
I have two arrays that I want to combine and then eliminate both values if duplicates appear. How can I achieve this?
arr1 = [1,2,3,4,5,6,7]
arr2 = [3,5,6,7]
I want to be left with
arr3 = [1,2,4]
Thanks
3
Answers
You can use the filter method to keep only those elements that are not duplicated. An element is considered a duplicate if it appears more than once in the combined array.
You can using the
filter
andincludes
array methods.Here’s an example:
Output:
I think you can use count sort for this method, after that you will come to know the frequency of elements occurring in the arrays (or simply merge them and see the frequency), than you can simply select the elements with frequency 1 in the count sort array or if using stack than stack.
Now take the unique items.
Sol 2: using Python
Simply merge the array (or make a list) than apply the unique property in the def unique(list1):
source: GFG