#Simangaliso Collin Mavuso collinmavuso98@gmail.com #QUESTION 3 #for this solution we used a simple approach by traversing over each element when we land #on an element with a count over 1 we return that value #NOTE :we can also solve it in other means that consider space complexities but lets keep it simple. def findDuplicate(dl): n=len(dl) for i in range(n): countGreaterThan1=dl.count(dl[i])# for each element get its occurance count if countGreaterThan1 > 1:# if its greater than 1 we have our answers return dl[i] # after weve iterated over all elemnts we by defalut return -1 when no countGreaterThan1 return -1 #Test Cases dl=[1,3,5,4,5,6,5,6456,64,4,46,6,6] cl=[2.3,4,54,2.3] ddl=[0] ccl=[1,2,3,4,5,6,7] print(findDuplicate(dl)) print(findDuplicate(cl)) print(findDuplicate(ddl)) print(findDuplicate(ccl))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question