Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How keep in place elements in array which are in second array

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

  1. Make a new array (In your case new_array);
  2. Loop around the array2.
  3. Inside the loop check if the element is present inside array.
  4. If present append to new_array.
  5. 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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading