Open In App

Python program to check if the list contains three consecutive common numbers in Python

Last Updated : 29 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Our task is to print the element which occurs 3 consecutive times in a Python list. 
Example :

Input : [4, 5, 5, 5, 3, 8]

Output : 5

Input : [1, 1, 1, 64, 23, 64, 22, 22, 22]

Output : 1, 22

Approach :

  1. Create a list.
  2. Create a loop for range size – 2.
  3. Check if the element is equal to the next element.
  4. Again check if the next element is equal to the next element.
  5. If both conditions are satisfied then print the element.

Example 1 : Only one occurrence of a 3 consecutively occurring element. 

Python3




# creating the array
arr = [4, 5, 5, 5, 3, 8]
 
# size of the list
size = len(arr)
 
# looping till length - 2
for i in range(size - 2):
 
    # checking the conditions
    if arr[i] == arr[i + 1] and arr[i + 1] == arr[i + 2]:
 
        # printing the element as the
        # conditions are satisfied
        print(arr[i])


Output : 

5

Time Complexity: O(n)
Auxiliary Space: O(n)

Example 2 : Multiple occurrences of 3 consecutively occurring elements. 

Python3




# creating the array
arr = [1, 1, 1, 64, 23, 64, 22, 22, 22]
 
# size of the list
size = len(arr)
 
# looping till length - 2
for i in range(size - 2):
 
    # checking the conditions
    if arr[i] == arr[i + 1] and arr[i + 1] == arr[i + 2]:
 
        # printing the element as the
        # conditions are satisfied
        print(arr[i])


Output : 

1
22

Time Complexity: O(n*n) where n is the number of elements in the list
Auxiliary Space: O(n), where n is the number of elements in the list 

Example #3: Using set() function

Python3




# creating the array
arr = [1, 1, 1, 64, 23, 64, 22, 22, 22]
 
# size of the list
size = len(arr)
 
# looping till length - 2
for i in range(size - 2):
    elements = set([arr[i], arr[i+1], arr[i+2]])
 
    if(len(elements) == 1):
            print(arr[i])


Output

1
22

Time Complexity: O(N)

Auxiliary Space : O(N)

Example #4:Using the itertools library:

Algorithm:

  1. Create a list of tuples of consecutive triplets from the input list using the zip() function.
  2. Use itertools.islice() to iterate over the list of triplets up to the second last element.
  3. Check if all three elements of the current triplet are equal.
  4. If they are equal, add the first element of the triplet to the output list.
  5. Return the output list.

Python3




import itertools
# creating the array
arr = [1, 1, 1, 64, 23, 64, 22, 22, 22]
triples = zip(arr, arr[1:], arr[2:])
print([x for x,y,z in itertools.islice(triples, len(arr)-2) if x==y==z])
#This code is contributed by Jyothi pinjala


Output

[1, 22]

Time Complexity:

Creating the list of triplets takes O(n) time, where n is the length of the input list.
The itertools.islice() function takes constant time to create an iterator over the list of triplets.
The loop over the list of triplets takes O(n) time, where n is the length of the input list.
Checking if all three elements of each triplet are equal takes constant time.
Adding an element to the output list takes constant time.
Overall, the time complexity of this code is O(n).

Auxiliary Space:

The list of triplets takes O(n) space, where n is the length of the input list.
The output list takes O(k) space, where k is the number of triplets where all three elements are equal.
Overall, the space complexity of this code is O(n + k).



Similar Reads

Python | Check if list contains consecutive numbers
Given a list of numbers, write a Python program to check if the list contains consecutive integers. Examples: Input : [2, 3, 1, 4, 5] Output : True Input : [1, 2, 3, 5, 6] Output : False Let's discuss the few ways we can do this task. Approach #1 : using sorted() This approach uses sorted() function of Python. We compare the sorted list with list o
3 min read
Python program to find common elements in three lists using sets
Prerequisite: Sets in Python Given three arrays, we have to find common elements in three sorted lists using sets. Examples : Input : ar1 = [1, 5, 10, 20, 40, 80] ar2 = [6, 7, 20, 80, 100] ar3 = [3, 4, 15, 20, 30, 70, 80, 120] Output : [80, 20] Input : ar1 = [1, 5, 5] ar2 = [3, 4, 5, 5, 10] ar3 = [5, 5, 10, 20] Output : [5] Method 1: We have given
5 min read
Python Check if the List Contains Elements of another List
In Python programming, there can be a case where we have to check whether a list contains elements of Another List or not. In that case, we can follow several approaches for doing so. In this article, we will be discussing several approaches to check whether a list contains elements of another list or not. Python List Contains Elements of another L
2 min read
Find common elements in three sorted arrays by dictionary intersection
One way to efficiently find shared items in three sorted arrays is by using dictionary intersection. However, it's important to note that dictionaries are commonly used for unique keys, so if there are duplicate elements in your arrays, some adjustments may be needed to make this approach work. Given three arrays sorted in non-decreasing order, pri
4 min read
Python | Check whether string contains only numbers or not
Given a string, write a Python program to find whether a string contains only numbers or not. Let's see a few methods to solve the above task. Check if String Contains Only Numbers Check if String Contains Only Numbers using isdigit() method Python String isdigit() method returns “True” if all characters in the string are digits, Otherwise, It retu
4 min read
Python | Check if list contains all unique elements
Some list operations require us to check if all the elements in the list are unique. This usually happens when we try to perform the set operations in a list. Hence this particular utility is essential at these times. Let's discuss certain methods by which this can be performed. Method #1: Naive Method Solutions usually start from the simplest meth
8 min read
Python program to verify that a string only contains letters, numbers, underscores and dashes
Given a string, we have to find whether the string contains any letters, numbers, underscores, and dashes or not. It is usually used for verifying username and password validity. For example, the user has a string for the username of a person and the user doesn't want the username to have any special characters such as @, $, etc. Prerequisite: Regu
4 min read
Python Program to calculate sum and average of three numbers
We are given three numbers, and our task is to calculate the sum and average of these numbers. The average of the numbers is defined as the sum of the given numbers divided by the total number of elements. In this case, it will be the sum of given three numbers divided by 3. Example: Input: 10, 20, 30Output: Sum=60, Average=20.0Input: 15, 29, 30Out
3 min read
Python - Filter the List of String whose index in second List contains the given Substring
Given two lists, extract all elements from the first list, whose corresponding index in the second list contains the required substring. Examples: Input : test_list1 = ["Gfg", "is", "not", "best", "and", "not", "CS"], test_list2 = ["Its ok", "all ok", "wrong", "looks ok", "ok", "wrong", "thats ok"], sub_str = "ok" Output : ['Gfg', 'is', 'best', 'an
10 min read
Program to check if a string contains any special character
Given a string, the task is to check if that string contains any special character (defined special character set). If any special character is found, don't accept that string. Examples : Input: Geeks$For$GeeksOutput: String is not accepted. Input: Geeks For GeeksOutput: String is accepted Approach 1: Using regular expression Make a regular express
14 min read