N = 2
sortidx = a.argsort()
_, id_arr = np.unique(a[sortidx], return_index=True)
valid_ind = np.unique((id_arr[:, (None)] + np.arange(N)).ravel().clip(max=a
    .size - 1))
out = a[np.sort(sortidx[valid_ind])]
numpy select fixed amount of values among duplicate values in array