Given a list of numbers and a particular element from the list, the task is to write a Python program to get the next unrevealed element to the first occurrence of a given element i.e element that does not occur till the given element.
Input : test_list = [3, 4, 6, 6, 3, 2, 3, 5, 6, 3, 4, 5, 5, 3, 6], nex_to_ele = 6
Output : 2
Explanation : In the above list there are 2, 3, 4, 5, and 6 are elements, as nex_to_ele is 6, whose first occurrence is at index 2, before that only 3 and 4 occurs, so the unrevealed elements are 2 and 5, out of which unrevealed element 2 is next after the first occurrence of 6. Hence 2 is the next unrevealed element of 6 in the list.
Input : test_list = [3, 4, 6, 6, 3, 2, 3, 5, 6, 3, 4, 5, 5, 3, 6], nex_to_ele = 3
Output : 4
Explanation : Next different unrevealed element to 3 is 4 as 3 occurs at index 0. So, none of the elements in the list have occurred before 3 and 4 is the next element after 3.
In this, the list is converted to a unique element list preserving order using set() and sorted(). Then index() is used to get the index of number and the next element to it in the result list is the required element.
The original list is : [3, 4, 6, 6, 3, 2, 3, 5, 6, 3, 4, 5, 5, 3, 6]
Next different element : 2
In this, we don't use index() to get element index, rather user iterator approach to increment to next the iterator till the element is found. The next element to it is result.
The original list is : [3, 4, 6, 6, 3, 2, 3, 5, 6, 3, 4, 5, 5, 3, 6]
Next different element : 2
OutputThe original list is : [3, 4, 6, 6, 3, 2, 3, 5, 6, 3, 4, 5, 5, 3, 6]
Next different element : 2
1.Initialize an empty dictionary 'freq' to store the frequency of elements.
2.Iterate through the list 'test_list' and update the frequency of each element in the 'freq' dictionary.
3.Find the index of the given element 'nex_to_ele' in 'test_list'.
4.Iterate through the list 'test_list' from index 'index+1' to the end and check if an element is not present in the 'freq' dictionary. The first such element encountered is the next different element.
5.If no such element is found, there is no next different element.
6.Print the next different element if found, else print a message indicating that there is no next different element.