Ask coding questions

← Back to all posts
Find duplicate number
NeuralNetwork

Hey i need help with a challenge i got about finding a duplicate number. the challeng is at the top of main.py.

The first thing i thought of broke a rule.

Can you help me?

Voters
workingaryan3
NeuralNetwork
Comments
hotnewtop
abc3354

Hello !
You sorted the array so yeah, you broke a rule :(
However your complexity is good 👍

If you want an other idea, you can loop on the array and for every number, try to find the same number somewhere else.
But this will be O(n^2)

Good luck ;)

NeuralNetwork

@abc3354 so like

def find(nums):
for i in range(len(nums)):
for j in range(len(nums)):
if nums[i] == nums[j] and i != j:
return nums[i]
break

this will always be less than O(n^2)

abc3354

@NeuralNetwork Yeah, I was thinking of that :)
But this will be O(n^2), I don't know if you can consider it as "less than O(n^2)"