In the previous article, we have discussed Python Program to Remove Elements from the Array/List Which Occurs More than k Times
Given a list, and the task is to print all the elements from a given list that appears more than once in python.
Examples:
Example1:
Input:
Given List = [20, 30, 40, 50, 20, 50]
Output:
The Elements from a given list [20, 30, 40, 50, 20, 50] that appears more than once : 20 50
Example2:
Input:
Given List = [15, 25, 35, 25, 15, 40]
Output:
The Elements from a given list [15, 25, 35, 25, 15, 40] that appears more than once : 15 25
Program for Array/List Elements that Appear More than Once in Python
Below are the ways to print all the elements from a given list that appears more than once in python:
- Using Dictionary (Hashing, User Input)
- Using Counter() function (Hashing, Static Input)
- Using Counter() function (Hashing, User Input)
- Using Counter() function (Hashing, User Input)
Method #1: Using Dictionary (Hashing, Static Input)
Approach:
- Take a dictionary and initialize it to empty using the {} or dict() say freqncyDictionary.
- Give the list as static input and store it in a variable.
- Loop in the given list using the For loop.
- Inside the For loop, Check if the list element is present in the dictionary or not using the if conditional statement and ‘in‘ keyword.
- If it is true then increment the count of the list element in the dictionary by 1.
- Else initialize the dictionary with the list element as key and value as 1.
- Take a new empty list say modifdlst and store it in a variable.
- Loop in the above-calculated frequency dictionary using the For loop
- Check if the key in the freqncyDictionary having a value greater than 1 using the if conditional statement.
- If it is true then append the key value to the above declared empty list modifdlst.
- Print all the elements from a given list that appears more than once.
- The Exit of the Program.
Below is the implementation:
# Take a dictionary and initialize it to empty
# using the {} or dict() say freqncyDictionary.
freqncyDictionary = {}
# Give the list as static input and store it in a variable.
gvnlst = [20, 30, 40, 50, 20, 50]
# Loop in the given list using the For loop.
for i in gvnlst:
# Inside the For loop,
# Check if the list element is present in the dictionary
# or not using the if conditional statement and 'in' keyword.
if i in freqncyDictionary.keys():
# If it is true then increment the count of the list element
# in the dictionary by 1.
freqncyDictionary[i] = freqncyDictionary[i]+1
# Else initialize the dictionary with the list element as key and value as 1.
else:
freqncyDictionary[i] = 1
# Take a new empty list say modifdlst and store it in a variable.
modifdlst = []
# Loop in the above calculated frequency dictionary using the For loop
for key in freqncyDictionary:
# Check if the key in the freqncyDictionary having value greater than 1
# using the if conditional statement.
if(freqncyDictionary[key] > 1):
# If it is true then append the key value to the above declared empty list modifdlst.
modifdlst.append(key)
# Print all the elements from a given list that appears more than once.
print("The Elements from a given list", gvnlst,
"that appears more than once : ")
print(*modifdlst)
Output:
The Elements from a given list [20, 30, 40, 50, 20, 50] that appears more than once : 20 50
Method #2: Using Dictionary (Hashing, User Input)
Approach:
- Take a dictionary and initialize it to empty using the {} or dict() say freqncyDictionary.
- Give the list as user input using the list(),map(),split(),int functions and store it in a variable.
- Loop in the given list using the For loop.
- Inside the For loop, Check if the list element is present in the dictionary or not using the if conditional statement and ‘in‘ keyword.
- If it is true then increment the count of the list element in the dictionary by 1.
- Else initialize the dictionary with the list element as key and value as 1.
- Take a new empty list say modifdlst and store it in a variable.
- Loop in the above-calculated frequency dictionary using the For loop
- Check if the key in the freqncyDictionary having a value greater than 1 using the if conditional statement.
- If it is true then append the key value to the above declared empty list modifdlst.
- Print all the elements from a given list that appears more than once.
- The Exit of the Program.
Below is the implementation:
# Take a dictionary and initialize it to empty
# using the {} or dict() say freqncyDictionary.
freqncyDictionary = {}
# Give the list as user input using the list(),map(),split(),int functions
# and store it in a variable.
gvnlst = list(
map(int, input('Enter some random list element separated by spaces = ').split()))
# Loop in the given list using the For loop.
for i in gvnlst:
# Inside the For loop,
# Check if the list element is present in the dictionary
# or not using the if conditional statement and 'in' keyword.
if i in freqncyDictionary.keys():
# If it is true then increment the count of the list element
# in the dictionary by 1.
freqncyDictionary[i] = freqncyDictionary[i]+1
# Else initialize the dictionary with the list element as key and value as 1.
else:
freqncyDictionary[i] = 1
# Take a new empty list say modifdlst and store it in a variable.
modifdlst = []
# Loop in the above calculated frequency dictionary using the For loop
for key in freqncyDictionary:
# Check if the key in the freqncyDictionary having value greater than 1
# using the if conditional statement.
if(freqncyDictionary[key] > 1):
# If it is true then append the key value to the above declared empty list modifdlst.
modifdlst.append(key)
# Print all the elements from a given list that appears more than once.
print("The Elements from a given list", gvnlst,
"that appears more than once : ")
print(*modifdlst)
Output:
Enter some random list element separated by spaces = 15 25 35 25 15 40 The Elements from a given list [15, 25, 35, 25, 15, 40] that appears more than once : 15 25
Method #3: Using Counter() function (Hashing, Static Input)
Approach:
- Import the Counter() function from collections using the import keyword.
- Give the list as static input and store it in a variable.
- Calculate the frequency of all the given list elements using the Counter() function which returns the element and its frequency as key-value pair and store this dictionary in a variable(say freqncyDictionary)
- Take a new empty list say modifdlst and store it in a variable.
- Loop in the above-calculated frequency dictionary using the For loop
- Check if the key in the freqncyDictionary having a value greater than 1 using the if conditional statement.
- If it is true then append the key value to the above declared empty list modifdlst.
- Print all the elements from a given list that appears more than once.
- The Exit of the Program.
Below is the implementation:
# Import the Counter() function from collections using the import keyword.
from collections import Counter
# Give the list as static input and store it in a variable.
gvnlst = [20, 30, 40, 50, 20, 50]
# Calculate the frequency of all the given list elements using the Counter()
# function which returns the element and its frequency as key-value pair
# and store this dictionary in a variable(say freqncyDictionary)
freqncyDictionary = Counter(gvnlst)
# Take a new empty list say modifdlst and store it in a variable.
modifdlst = []
# Loop in the above calculated frequency dictionary using the For loop
for key in freqncyDictionary:
# Check if the key in the freqncyDictionary having value greater than 1
# using the if conditional statement.
if(freqncyDictionary[key] > 1):
# If it is true then append the key value to the above declared empty list modifdlst.
modifdlst.append(key)
# Print all the elements from a given list that appears more than once.
print("The Elements from a given list", gvnlst,
"that appears more than once : ")
print(*modifdlst)
Output:
The Elements from a given list [20, 30, 40, 50, 20, 50] that appears more than once : 20 50
Method #4: Using Counter() function (Hashing, User Input)
Approach:
- Import the Counter() function from collections using the import keyword.
- Give the list as user input using the list(),map(),split(),int functions and store it in a variable.
- Calculate the frequency of all the given list elements using the Counter() function which returns the element and its frequency as key-value pair and store this dictionary in a variable(say freqncyDictionary)
- Take a new empty list say modifdlst and store it in a variable.
- Loop in the above-calculated frequency dictionary using the For loop
- Check if the key in the freqncyDictionary having a value greater than 1 using the if conditional statement.
- If it is true then append the key value to the above declared empty list modifdlst.
- Print all the elements from a given list that appears more than once.
- The Exit of the Program.
Below is the implementation:
# Import the Counter() function from collections using the import keyword.
from collections import Counter
# Give the list as user input using the list(),map(),split(),int functions
# and store it in a variable.
gvnlst = list(
map(int, input('Enter some random list element separated by spaces = ').split()))
# Calculate the frequency of all the given list elements using the Counter()
# function which returns the element and its frequency as key-value pair
# and store this dictionary in a variable(say freqncyDictionary)
freqncyDictionary = Counter(gvnlst)
# Take a new empty list say modifdlst and store it in a variable.
modifdlst = []
# Loop in the above calculated frequency dictionary using the For loop
for key in freqncyDictionary:
# Check if the key in the freqncyDictionary having value greater than 1
# using the if conditional statement.
if(freqncyDictionary[key] > 1):
# If it is true then append the key value to the above declared empty list modifdlst.
modifdlst.append(key)
# Print all the elements from a given list that appears more than once.
print("The Elements from a given list", gvnlst,
"that appears more than once : ")
print(*modifdlst)
Output:
Enter some random list element separated by spaces = 1 2 4 4 2 3 6 1 The Elements from a given list [1, 2, 4, 4, 2, 3, 6, 1] that appears more than once : 1 2 4
Access the big list of Python Programming Code Examples with actual logical code asked in Programming and Coding Interviews for Python and stand out from the crowd.
