1

How can I find the repeated value in a list? There will always be only one repeated value

for example:

numbers=[1,2,3,4,5,3]

I need to get the value 3

This is what I was trying but it is sometimes printing the value of the same list 2 times.

endLoop=False
    for n1 in range(0,len(numbers)):
        for n2 in range(1,len(numbers)):
            if numbers[n1]==numbers[n2]:
                print numbers
                print numbers[n1]
                endLoop=True
        if endLoop:
            break
FutoRicky
  • 903
  • 2
  • 9
  • 22
  • What should happen if there is more than one repeated value? No repeated values? Does it matter if values are repeated more often than others? – Martijn Pieters Aug 28 '14 at 19:29
  • There will always be only 1 repeated value. Thanks for the comment, editing the post now. – FutoRicky Aug 28 '14 at 19:30
  • http://stackoverflow.com/questions/2161752/how-to-count-the-frequency-of-the-elements-in-a-list ? – Marc B Aug 28 '14 at 19:30

7 Answers7

7

Keep track of numbers you have seen with a set() object, the first number that is already in the set is repeated:

def find_repeat(numbers):
    seen = set()
    for num in numbers:
        if num in seen:
            return num
        seen.add(num)

This is an efficient method to find the first repeated value, as it won't have to look at the rest of the numbers once it finds it.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
6

there is a simpler method to do so. using .count() keyword. you can do something like this

for n in numbers:
    if numbers.count(n)>1:
        print("Repeated number: ",n)
2

If you don't mind using numpy:

numbers = [1,2,3,4,5,3]
counts = np.bincount( numbers )
np.where([ counts > 1 ])[1]

... Will get you array([3])

Lukeclh
  • 231
  • 1
  • 5
  • This does require a full scan again. It can be solved with less work because the OP only needs the *first* repeated number. – Martijn Pieters Aug 28 '14 at 19:49
  • You are quite right. This solution work well only if the array is reasonably short and/or speed is not a big issue. – Lukeclh Aug 28 '14 at 20:00
1

You should check that n1 != n2

because your program will check numbers[1] == numbers[1] and print the value at the position 1, even there is only one vaulue equal to numbers[1]

so the code will look like:

for n1 in range(0,len(numbers)):
    for n2 in range(1,len(numbers)):
        if numbers[n1]==numbers[n2] and n1 != n2:
            print numbers
            print numbers[n1]
            endLoop=True
    if endLoop:
        break
ivan.mylyanyk
  • 2,051
  • 4
  • 30
  • 37
1

Here is a one liner.

set([x for x in _list if _list.count(x) > 1])
rocktheartsm4l
  • 2,129
  • 23
  • 38
  • This is *very* inefficient; each `test.count()` call does a full scan of the list to count the elements. You are also repeating the count for any repeated element. – Martijn Pieters Aug 28 '14 at 19:37
0
g=int(input("how many digits?"))
n=int(input("enter a number"))
c=0
k=0
k=n
for i in range(0,g):
    While  (n!=0):
               r=n%10
               n=n//10
               r1=n%10
               If (r==r1):
                               c=c+1
 if (c>0):
     print("reparation is there")
 else:
     print("no repeatation .....")
  • Welcome to stackoverflow. This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. – Kenzo_Gilead Aug 28 '17 at 12:44
0

A variant to Lukeclh s' answer

numbers = [1,2,3,4,5,3]
counts = np.bincount( numbers )
np.where( counts > 1 )[0]

will give the same result

fenaux
  • 37
  • 5