You can save indexes with elements ( zip ) and sort and return the element in the middle or two elements in the middle, however the sort will be O(n.logn) . The following method is O(n) in terms of time complexity.
import numpy as np def arg_median(a): if len(a) % 2 == 1: return np.where(a == np.median(a))[0][0] else: l,r = len(a)
Output:
2 [2, 1]
The idea is that if there is only one median (the array has an odd length), then it returns the median index. If we need to perform averaging over elements (the array has an even length), it returns the indices of these two elements in the list.
source share