You are given a read-only array of n integers from 1 to n. Each integer appears exactly once except A which appears twice and B which is missing.
Return A and B.
Input:[3 1 2 5 3] Output:[3, 4] A = 3, B = 4
I wrote the following code:
def repeated_number(number_array): pair_A_B =  n = len(number_array) for i in number_array: if(number_array.count(i)==2): pair_A_B.append(i) break sample = range(1,n+1) diff = list(set(sample)-set(number_array)) pair_A_B.append(diff) return pair_A_B sample_input = [3,1,2,3,5] print(repeated_number(sample_input))
The code works fine for this problem on my laptop but when I try to submit it on a coding forum it says my code is not efficient. How can I make it efficient in terms of time?