I am a beginner and I was practicing a question on hackerrank. I wrote this code as part of a problem which timed out for large inputs:
K = int(input())
roomnos = input().split()
setroomnos = set(roomnos)
for r in setroomnos:
if roomnos.count(r) == 1:
print(r)
break
The next one was accepted by the judge for all test cases
K = int(input())
roomnos = [int(i) for i in input().split()]
setroomnos = set(roomnos)
c = (K * sum(setroomnos) - sum(roomnos)) // (K - 1)
print(c)
can you please explain why the first one timed out for large inputs and the second one worked fine PS: The fundamental operation is to find a no which appears only once in a list as opposed to other nos which appear K times