+ 2
how to find the repeated item of a tuple? tup=(2,4,5,6,2,3,4,4,7,8,4,3,2) expecting output:4
13 Answers
+ 16
for i in tup:
if tup.count(i) > 1:
print('REPEATED')
I think this is the shortest solution
+ 11
print(tup.count(4)) # outputs 4 because there are 4 fours
+ 8
This is perfect :
https://code.sololearn.com/cQMR30h0jaYy/?ref=app
#Don't forget to like the code. 😉😁😉
+ 8
@Tobi Yes! I have just started learning python. Will learn every module completely.
Oh! And they does not teach lambda or Linq in C# here. You will need to learn it on the MSDN. Very few programmers use these techniques. So less tutorials out there for advance programming. 😔
+ 7
@Tobi Wow! There is also lambda expression in python too!! Lambda makes codes much smaller and efficient. I always use lambda expression in C#. 😉
+ 6
Perhaps use a loop that would find out how many repeats a number has (maybe with an array). Then you can just find the number with the most repeats.
Or use 2 nested loops, one to check number of repeats, one to go through the other loop again to see if there's a larger repeat. (easier method but O(n^2) complexity.)
Edit:
@mary sorry I've never done python so i wouldn't be able to help with writing the actual code.
+ 3
@Cyrus: Yeah, I love all these concepts from functional programming too. Python has quite a few cool concepts like these, check out the functional programming module in the course, if you like. SoloLearn really did a great job introducing them.
I will definitely look into C#, when the time has come.
+ 2
This gives you one of the list elements, which are repeated the most. Essentially one line and not to inelegant:
https://code.sololearn.com/c0p3TH5wQ3w9/?ref=app
+ 1
Why 4 and not 2? 2 is also repeated.
+ 1
@Tobi maybe the question wants the most repeated.
0
@Rrestoring faith
it's too complicated for me……
0
okay,thank you all
0
#This finds the repeated items in a tuple....
tup= (1,2,3,4,4,5,5,12,12,12,12,1,3,2,4, "my", "my", "hi", "hi", "hello")
rep=[]
for i in tup:
if tup.count(i)>1:
rep.append(i)
else:
continue
for x in rep:
while rep.count(x)>1:
rep.remove(x)
print("Repeated items:",tuple(rep))