{"id":18463,"date":"2021-08-26T10:39:23","date_gmt":"2021-08-26T05:09:23","guid":{"rendered":"https:\/\/python-programs.com\/?p=18463"},"modified":"2021-11-22T18:37:16","modified_gmt":"2021-11-22T13:07:16","slug":"python-program-for-maximum-distance-between-two-occurrences-of-same-element-in-array-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-maximum-distance-between-two-occurrences-of-same-element-in-array-list\/","title":{"rendered":"Python Program for Maximum Distance between Two Occurrences of Same Element in Array\/List"},"content":{"rendered":"
In the previous article, we have discussed Python Program for Leonardo Number Series<\/a> Python dictionaries are mutable collections of things that contain key-value pairs. The dictionary contains two main components: keys and values. These keys must be single elements, and the values can be of any data type, such as list, string, integer, tuple, and so on. The keys are linked to their corresponding values. In other words, the values can be retrieved using their corresponding keys.<\/p>\n A dictionary is created in Python by enclosing numerous key-value pairs in curly braces.<\/p>\n Given a list, the task is to find the greatest distance between two occurrences of an element in a given list of repeated elements.<\/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 are the ways to find the greatest distance between two occurrences of an element in a given list of repeated elements:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program for Leonardo Number Series Dictionaries: Python dictionaries are mutable collections of things that contain key-value pairs. The dictionary contains two main components: keys and values. These keys must be single elements, and the values can be of any data type, such as list, string, integer, tuple, …<\/p>\n
\nDictionaries:<\/strong><\/p>\nGiven List = [1, 2, 3, 7, 8, 9, 2, 1, 4, 3]<\/pre>\n
The greatest distance between two occurrences of an element in a given list of repeated elements = 7<\/pre>\n
Given List = [6, 7, 8, 2, 1, 6, 8, 9]<\/pre>\n
The greatest distance between two occurrences of an element in a given list of repeated elements = 5<\/pre>\n
Program for Maximum Distance between Two Occurrences of the Same Element in Array\/List in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [1, 2, 3, 7, 8, 9, 2, 1, 4, 3]\r\n# Calculate the length of the given list using the len() function and store it in\r\n# another variable.\r\nlst_len = len(gvn_lst)\r\n# Create an empty dictionary and store it in another variable.\r\nnew_dict = {}\r\n# Take a variable say \"max_val\" and initialize its value with 0.\r\nmax_val = 0\r\n# Loop till the number of elements of the list using the for loop.\r\nfor itr in range(lst_len):\r\n # Check if the list element is present in the dictionary keys or not using the\r\n # if conditional statement.\r\n if gvn_lst[itr] not in new_dict.keys():\r\n # If the statement is true, then update the dictionary with the key as a list element\r\n # and value as an iterator.\r\n new_dict[gvn_lst[itr]] = itr\r\n # If it is not true, calculate the maximum value of \"max_val\" and iterator -dictionary\r\n# of list of iterator and store it in \"max_val\".\r\n else:\r\n max_val = max(max_val, itr-new_dict[gvn_lst[itr]])\r\n # Print \"max_val\" to get the greatest distance between two occurrences of an element\r\n# in a given list of repeated elements.\r\nprint(\"The greatest distance between two occurrences of an element in a given list of repeated elements = \", max_val)\r\n<\/pre>\n
The greatest distance between two occurrences of an element in a given list of repeated elements = 7<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n#Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Calculate the length of the given list using the len() function and store it in\r\n# another variable.\r\nlst_len = len(gvn_lst)\r\n# Create an empty dictionary and store it in another variable.\r\nnew_dict = {}\r\n# Take a variable say \"max_val\" and initialize its value with 0.\r\nmax_val = 0\r\n# Loop till the number of elements of the list using the for loop.\r\nfor itr in range(lst_len):\r\n # Check if the list element is present in the dictionary keys or not using the\r\n # if conditional statement.\r\n if gvn_lst[itr] not in new_dict.keys():\r\n # If the statement is true, then update the dictionary with the key as a list element\r\n # and value as an iterator.\r\n new_dict[gvn_lst[itr]] = itr\r\n # If it is not true, calculate the maximum value of \"max_val\" and iterator -dictionary\r\n# of list of iterator and store it in \"max_val\".\r\n else:\r\n max_val = max(max_val, itr-new_dict[gvn_lst[itr]])\r\n # Print \"max_val\" to get the greatest distance between two occurrences of an element\r\n# in a given list of repeated elements.\r\nprint(\"The greatest distance between two occurrences of an element in a given list of repeated elements = \", max_val)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 6 7 8 2 1 6 8 9\r\nThe greatest distance between two occurrences of an element in a given list of repeated elements = 5<\/pre>\n
\n