1

Basically I want to find and print the positions of all the duplicate elements. Here is my code:

numbers = [7,1,3,6,4,2,5,9,8,10]
n = int(input("Enter a number from 1 to 10 to search for the number's position: "))

def findNum():
    count = 0
    while numbers[count] != n:
        count += 1

    numPos = count + 1
    print(numPos)


if n in numbers:
    print("Calculating position... (takes 9 years so wait)")
    findNum()
    
else:
    print("Number not found, next time enter a number from 1 to 10!")

For example I add an extra 7 to numbers:

numbers = [7,1,3,6,4,2,5,9,8,10,7]

Then I want to return the 1st 7's position and also the other 7's position. How to do it?

rustyhu
  • 1,912
  • 19
  • 28
  • `index=[idx for idx,val in enumerate(some_list) if val==n]` – Ch3steR Oct 17 '21 at 15:23
  • @Ch3steR I don't think that duplicate is appropriate – Dani Mesejo Oct 17 '21 at 15:29
  • @DaniMesejo OP takes `n` as input, wants to print indices of all occurrences of `n`. The dupe I linked does the same except it find indices of `min` value? Or maybe I understood the question? – Ch3steR Oct 17 '21 at 15:34
  • @Ch3steR I understand your reasoning and sure both questions are about finding indices, but as you said one finds the indices o minimum so only one sets of indices (minima?) available, when face with duplicates you can multiple sets of indices one for each duplicates. – Dani Mesejo Oct 17 '21 at 15:36
  • @DaniMesejo But from OP's code i think it's quite evident that he wants indices of only `n`. IIUC, you are saying this what OP wants `L = [7, 7, 8, 8]` -> `{7:[0, 1], 8:[2, 3]}`? I think OP want `[0, 1]` if `n` is 7, if `n` is 8 `[2, 3]`. I think your answer is great as usual and an overkill here. +1 too. I might be wrong English is not my native language. I'm happy to reopen the question. – Ch3steR Oct 17 '21 at 15:41
  • @DaniMesejo You are more experienced than me. I'm reopening, I think you are a better judge than me. I'll leave the link to the other post as well. – Ch3steR Oct 17 '21 at 15:45
  • Related: https://stackoverflow.com/questions/60501140/how-to-determine-several-minimum-in-a-list – Ch3steR Oct 17 '21 at 15:45
  • @Ch3steR Thanks for being so understanding and taking the time to answer. Really appreciate it. – Dani Mesejo Oct 17 '21 at 15:46

2 Answers2

3

To get all duplicates use a dictionary with the keys as the numbers in the list and the values the positions, to get the positions use enumerate:

from collections import defaultdict

numbers = [7, 1, 3, 6, 4, 2, 5, 9, 8, 10, 7]
duplicates = defaultdict(list)

# iterate over positions and numbers simultaneously
for i, number in enumerate(numbers):
    # accumulate positions to the same number
    duplicates[number].append(i)

result = {key: value for key, value in duplicates.items() if len(value) > 1}
print(result)

Output

{7: [0, 10]}

As you can see from the output it returns that 7 appears in position 0 an 10. The overall complexity of this approach is O(n).

The loop:

# iterate over positions and numbers simultaneously
for i, number in enumerate(numbers):
    # accumulate positions to the same number
    duplicates[number].append(i)

group the different positions (i in the code) by the same number. Using a defaultdict.

The expression:

result = {key: value for key, value in duplicates.items() if len(value) > 1}

is a dictionary comprehension, see some more info here.

Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76
0
numbers = [7,1,7,6,4,2,5,9,8,10,7]
m=[]
for i in range(len(numbers)):
    for j in range(i+1,len(numbers),1):
        if(numbers[i]==numbers[j]):
            m.append(i)
            m.append(j)
l=list(set(m))
for i in range(len(l)):
    print("First Occurence at position:{}".format(l[i]))