I have two arrays and I need to create new array which include elements which are in first array have such as here:
array1 = [1,2,3]
array2 = [1,3,6,3,8,2,2,3,3]
my_func(arr1, arr2):
...
return new_array
print(myfunc(array1, array2)
Output: [1,3,3,2,2,3,3]
>Solution :
You could answer this by looping around the array2 and checking if the each element is present in array1 if yes then append to new_array else go on with loop.
- Make a new array (In your case new_array);
- Loop around the array2.
- Inside the loop check if the element is present inside array.
- If present append to new_array.
- After loop end return the new_array.
If you are not able To understand above logic please refer to code below and try to understand from it.
array1 = [1,2,3]
array2 = [1,3,6,3,8,2,2,3,3]
def my_func(arr1, arr2):
new_array = []
for i in range(len):
if(i in arr1):
new_array.append(i)
return new_array
print(my_func(array1, array2))