{"id":10671,"date":"2021-09-30T16:00:36","date_gmt":"2021-09-30T10:30:36","guid":{"rendered":"https:\/\/python-programs.com\/?p=10671"},"modified":"2021-11-22T18:34:25","modified_gmt":"2021-11-22T13:04:25","slug":"python-program-to-find-the-minimum-index-of-a-repeating-element-in-an-array-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-minimum-index-of-a-repeating-element-in-an-array-list\/","title":{"rendered":"Python Program to Find the Minimum Index of a Repeating Element in an Array\/List"},"content":{"rendered":"
Don’t miss the chance of Java programs examples with output pdf free download<\/a> as it is very essential for all beginners to experienced programmers for cracking the interviews.<\/p>\n Lists in Python:<\/strong><\/p>\n Lists are one of Python\u2019s 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.<\/p>\n Given a list\/array, the task is to find the minimum index of the repeating element in the given list in Python.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below is the full approach for finding the minimum index of the repeating element in the given list in Python<\/p>\n Counter() function:<\/strong><\/p>\n 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.<\/p>\n The counter is a subclass that counts hashable objects. When called, it constructs an iterable hash table implicitly.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Since we iterate only once the Time Complexity of the above approach is O(n).<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Since we iterate only once the Time Complexity of the above approach is O(n). 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\u2019s most commonly used built-in data structures. You can make a list by putting all of the elements inside square …<\/p>\ngiven list = [8, 12, 38, 7, 1, 9, 19, 11, 45, 62, 57, 18, 12, 32, 45, 7, 1]<\/pre>\n
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] :\r\n 1<\/pre>\n
given list =[7, 86, 23, 96, 11, 23, 45, 78, 96, 23, 79, 123, 456, 789]<\/pre>\n
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] :\r\n2<\/pre>\n
Program to Find the Minimum Index of a Repeating Element in an Array\/List in Python<\/h2>\n
\n
1)Using Hashing with Counter() function(Static Input)<\/h3>\n
\n
# Import the Counter function from the collections module.\r\nfrom collections import Counter\r\n# Give the list as static input and store it in a variable.\r\ngiven_list = [8, 12, 38, 7, 1, 9, 19, 11, 45, 62, 57, 18, 12, 32, 45, 7, 1]\r\n# Calculate the frequency of all the elements of the given list\r\n# using the Counter() function and store it in a variable.\r\nelemeFreq = Counter(given_list)\r\n# Traverse the given list using For loop.\r\nfor indeval in range(len(given_list)):\r\n # Check if the element has a frequency greater than 1 using the if statement.\r\n if(elemeFreq[given_list[indeval]] > 1):\r\n # If it has a frequency greater than 1, then print the iterator value of the loop\r\n # (which is the minimum index of the repeating element)\r\n print('The minimum index of the repeating element of the given list',\r\n given_list, ':\\n', indeval)\r\n # Break the loop using the break statement\r\n break\r\n<\/pre>\n
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] :\r\n 1<\/pre>\n
2)Using Hashing with Counter() function(User Input)<\/h3>\n
\n
# Import the Counter function from the collections module.\r\nfrom collections import Counter\r\n# Give the list as user input using split(),int,map() and list() functions.\r\n# Store it in a variable.\r\ngiven_list = list(\r\n map(int, input('Enter some random list elements separated by spaces = ').split()))\r\n# Calculate the frequency of all the elements of the given list\r\n# using the Counter() function and store it in a variable.\r\nelemeFreq = Counter(given_list)\r\n# Traverse the given list using For loop.\r\nfor indeval in range(len(given_list)):\r\n # Check if the element has a frequency greater than 1 using the if statement.\r\n if(elemeFreq[given_list[indeval]] > 1):\r\n # If it has a frequency greater than 1, then print the iterator value of the loop\r\n # (which is the minimum index of the repeating element)\r\n print('The minimum index of the repeating element of the given list',\r\n given_list, ':\\n', indeval)\r\n # Break the loop using break statement\r\n break\r\n<\/pre>\n
Enter some random list elements separated by spaces = 7 86 23 96 11 23 45 78 96 23 79 123 456 789\r\nThe minimum index of the repeating element of the given list [7, 86, 23, 96, 11, 23, 45, 78, 96, 23, 79, 123, 456, 789] :\r\n2<\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n