Program for Maximum Distance between Two Occurrences of Same Element in ArrayList

Python Program for Maximum Distance between Two Occurrences of Same Element in Array/List

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, and so on. The keys are linked to their corresponding values. In other words, the values can be retrieved using their corresponding keys.

A dictionary is created in Python by enclosing numerous key-value pairs in curly braces.

Given a list, the task is to find the greatest distance between two occurrences of an element in a given list of repeated elements.

Examples:

Example1:

Input:

Given List =  [1, 2, 3, 7, 8, 9, 2, 1, 4, 3]

Output:

The greatest distance between two occurrences of an element in a given list of repeated elements = 7

Example2:

Input:

Given List = [6, 7, 8, 2, 1, 6, 8, 9]

Output:

The greatest distance between two occurrences of an element in a given list of repeated elements = 5

Program for Maximum Distance between Two Occurrences of the Same Element in Array/List in Python

Below are the ways to find the greatest distance between two occurrences of an element in a given list of repeated elements:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Create an empty dictionary and store it in another variable.
  • Take a variable say “max_val” and initialize its value with 0.
  • Loop till the number of elements of the list using the for loop.
  • Check if the list element is present in the dictionary keys or not using the if conditional statement.
  • If the statement is true, then update the dictionary with the key as a list element and value as an iterator.
  • If it is not true, calculate the maximum value of “max_val” and iterator -dictionary of list of iterator and store it in “max_val”.
  • Print “max_val” to get the greatest distance between two occurrences of an element in a given list of repeated elements.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [1, 2, 3, 7, 8, 9, 2, 1, 4, 3]
# Calculate the length of the given list using the len() function and store it in
# another variable.
lst_len = len(gvn_lst)
# Create an empty dictionary and store it in another variable.
new_dict = {}
# Take a variable say "max_val" and initialize its value with 0.
max_val = 0
# Loop till the number of elements of the list using the for loop.
for itr in range(lst_len):
    # Check if the list element is present in the dictionary keys or not using the
    # if conditional statement.
    if gvn_lst[itr] not in new_dict.keys():
        # If the statement is true, then update the dictionary with the key as a list element
        # and value as an iterator.
        new_dict[gvn_lst[itr]] = itr
 # If it is not true, calculate the maximum value of "max_val" and iterator -dictionary
# of list of iterator and store it in "max_val".
    else:
        max_val = max(max_val, itr-new_dict[gvn_lst[itr]])
 # Print "max_val" to get the greatest distance between two occurrences of an element
# in a given list of repeated elements.
print("The greatest distance between two occurrences of an element in a given list of repeated elements = ", max_val)

Output:

The greatest distance between two occurrences of an element in a given list of repeated elements =  7

Method #2: Using For loop (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Create an empty dictionary and store it in another variable.
  • Take a variable say “max_val” and initialize its value with 0.
  • Loop till the number of elements of the list using the for loop.
  • Check if the list element is present in the dictionary keys or not using the if conditional statement.
  • If the statement is true, then update the dictionary with the key as a list element and value as an iterator.
  • If it is not true, calculate the maximum value of “max_val” and iterator -dictionary of list of iterator and store it in “max_val”.
  • Print “max_val” to get the greatest distance between two occurrences of an element in a given list of repeated elements.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
#Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Calculate the length of the given list using the len() function and store it in
# another variable.
lst_len = len(gvn_lst)
# Create an empty dictionary and store it in another variable.
new_dict = {}
# Take a variable say "max_val" and initialize its value with 0.
max_val = 0
# Loop till the number of elements of the list using the for loop.
for itr in range(lst_len):
    # Check if the list element is present in the dictionary keys or not using the
    # if conditional statement.
    if gvn_lst[itr] not in new_dict.keys():
        # If the statement is true, then update the dictionary with the key as a list element
        # and value as an iterator.
        new_dict[gvn_lst[itr]] = itr
 # If it is not true, calculate the maximum value of "max_val" and iterator -dictionary
# of list of iterator and store it in "max_val".
    else:
        max_val = max(max_val, itr-new_dict[gvn_lst[itr]])
 # Print "max_val" to get the greatest distance between two occurrences of an element
# in a given list of repeated elements.
print("The greatest distance between two occurrences of an element in a given list of repeated elements = ", max_val)

Output:

Enter some random List Elements separated by spaces = 6 7 8 2 1 6 8 9
The greatest distance between two occurrences of an element in a given list of repeated elements = 5

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.