Home > Net >  Finding second occurrence of a number in a given list using python program
Finding second occurrence of a number in a given list using python program

Time:01-23

I'm writing a Python program to find the position of the second occurrence(index) of a given number in a given list of numbers. The function will take as input a list of numbers as the first argument and a numeric variable as the second argument. This function should return the index where the given variable value occurs in the list for the second time If the number does not occur for the second time in the input list or If the number does not exist, the function should return 0.

    def getIndex(listOfIntegers,NumericVariable):
        inp=int(NumericVariable)
        for i in listOfIntegers:
            if i==inp:
                b=(listOfIntegers.index(i))
                c=b 1
        
        if c<len(listOfIntegers):
            y=listOfIntegers[c:]

            for j in y:
                if j == inp:
                    d=(y.index(j))
                    res=d c
                    
                    return res
                continue
                
        else:
            return 0     
    if __name__ =='__main__':
        l1=[]
        size=int(input())
        for i in range(size):
            l1.append(int(input()))
        num=int(input())
        output=getIndex(l1,num)
        print(output)

This is the code I used although I get the results as expected, it says some of the test cases failed. Please suggest to me what could have gone wrong or how can I improve this code. Thank You

CodePudding user response:

You could use enumerate(), which assigns the index value of the current element to i. Also, if the requested element is not found for the 2nd time, then the else clause of the for loop returns 0.

def getIndex(int_list, num):
  count = 0
  for i, n in enumerate(int_list):
    if n == num:
      count  = 1
      if count == 2:
        return i
  else:
    return 0

print(getIndex([1, 2, 3, 4, 5, 4], 4))

Output:

5

CodePudding user response:

I think you got the wrong approach, you need to rethink the logic, you don't need to make 2 loops, you can just use a counter to count the number of occurence you find, until it reaches the targeted number of occurence

Here is how I would have implemented it:

def find_in_list(int_list, n_to_find, occurence=2):
    i = 0
    while i < len(int_list):
        if (int_list[i] == n_to_find):
            occurence -= 1
            if (occurence == 0):
                return i
        i  = 1
    return 0

If that answers your question, please don't forget the vote up my answer, and to validated it by clicking on the checkmark on the left.

CodePudding user response:

Other version:

def getIndex(listOfIntegers, NumericVariable):
    occurrence = 0
    for index, number in enumerate(listOfIntegers):
        if number == NumericVariable:
            occurrence  = 1
            if occurrence == 2:
                return index
    return 0
  •  Tags:  
  • Related