Get index of minimum python – Python Program to Find the Minimum Index of a Repeating Element in an Array/List

Get index of minimum python: Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Lists in Python:

Lists are one of Python’s most commonly used built-in data structures. You can make a list by putting all of the elements inside square brackets[ ] and separating them with commas. Lists can include any type of object, making them extremely useful and adaptable.

Given a list/array, the task is to find the minimum index of the repeating element in the given list in Python.

Examples:

Example1:

Input:

given list = [8, 12, 38, 7, 1, 9, 19, 11, 45, 62, 57, 18, 12, 32, 45, 7, 1]

Output:

The minimum index of the repeating element of the given list [8, 12, 38, 7, 1, 9, 19, 11, 45, 62, 57, 18, 12, 32, 45, 7, 1] :
 1

Example2:

Input:

given list =[7, 86, 23, 96, 11, 23, 45, 78, 96, 23, 79, 123, 456, 789]

Output:

The minimum index of the repeating element of the given list [7, 86, 23, 96, 11, 23, 45, 78, 96, 23, 79, 123, 456, 789] :
2

Program to Find the Minimum Index of a Repeating Element in an Array/List in Python

Below is the full approach for finding the minimum index of the repeating element in the given list in Python

1)Using Hashing with Counter() function(Static Input)

Counter() function:

The Counter class is a subset of the object data-set offered by Python3’s collections module. The Collections module provides the user with specialized container datatypes, acting as an alternative to Python’s general-purpose built-ins such as dictionaries, lists, and tuples.

The counter is a subclass that counts hashable objects. When called, it constructs an iterable hash table implicitly.

Approach:

  • Import the Counter function from the collections module.
  • Give the list as static input and store it in a variable.
  • Calculate the frequency of all the elements of the given list using the Counter() function and store it in a variable.
  • Traverse the given list using For loop.
  • Check if the element has a frequency greater than 1 using the if statement.
  • If it has a frequency greater than 1, then print the iterator value of the loop(which is the minimum index of the repeating element)
  • Break the loop using a break statement
  • The Exit of the Program.

Below is the implementation:

# Import the Counter function from the collections module.
from collections import Counter
# Give the list as static input and store it in a variable.
given_list = [8, 12, 38, 7, 1, 9, 19, 11, 45, 62, 57, 18, 12, 32, 45, 7, 1]
# Calculate the frequency of all the elements of the given list
# using the Counter() function and store it in a variable.
elemeFreq = Counter(given_list)
# Traverse the given list using For loop.
for indeval in range(len(given_list)):
    # Check if the element has a frequency greater than 1 using the if statement.
    if(elemeFreq[given_list[indeval]] > 1):
        # If it has a frequency greater than 1, then print the iterator value of the loop
        # (which is the minimum index of the repeating element)
        print('The minimum index of the repeating element of the given list',
              given_list, ':\n', indeval)
        # Break the loop using the break statement
        break

Output:

The minimum index of the repeating element of the given list [8, 12, 38, 7, 1, 9, 19, 11, 45, 62, 57, 18, 12, 32, 45, 7, 1] :
 1

Since we iterate only once the Time Complexity of the above approach is O(n).

2)Using Hashing with Counter() function(User Input)

Approach:

  • Import the Counter function from the collections module.
  • Give the list as user input using split(),int,map() and list() functions.
  • Store it in a variable.
  • Calculate the frequency of all the elements of the given list using the Counter() function and store it in a variable.
  • Traverse the given list using For loop.
  • Check if the element has a frequency greater than 1 using the if statement.
  • If it has a frequency greater than 1, then print the iterator value of the loop(which is the minimum index of the repeating element)
  • Break the loop using a break statement
  • The Exit of the Program.

Below is the implementation:

# Import the Counter function from the collections module.
from collections import Counter
# Give the list as user input using split(),int,map() and list() functions.
# Store it in a variable.
given_list = list(
    map(int, input('Enter some random list elements separated by spaces = ').split()))
# Calculate the frequency of all the elements of the given list
# using the Counter() function and store it in a variable.
elemeFreq = Counter(given_list)
# Traverse the given list using For loop.
for indeval in range(len(given_list)):
    # Check if the element has a frequency greater than 1 using the if statement.
    if(elemeFreq[given_list[indeval]] > 1):
        # If it has a frequency greater than 1, then print the iterator value of the loop
        # (which is the minimum index of the repeating element)
        print('The minimum index of the repeating element of the given list',
              given_list, ':\n', indeval)
        # Break the loop using break statement
        break

Output:

Enter some random list elements separated by spaces = 7 86 23 96 11 23 45 78 96 23 79 123 456 789
The minimum index of the repeating element of the given list [7, 86, 23, 96, 11, 23, 45, 78, 96, 23, 79, 123, 456, 789] :
2

Since we iterate only once the Time Complexity of the above approach is O(n).
Related Programs: