In the previous article, we have discussed Python Program to Count Words that Appear Exactly Two Times in an Array/List of Words
Counter function in Python:
The counter is a set and dict subset. Counter() takes an iterable entity as an argument and stores the elements as keys and the frequency of the elements as a value. So, in collections, if we transfer a string. When you call Counter(), you’ll get a Counter class object with characters as keys and their frequency in a string as values.
Counter() returns a Counter type object (a subclass of dict) with all characters in the string as keys and their occurrence count as values. We’ll use the [] operator to get the occurrence count of the characters from it.
Given a list, and the task is to find two odd occurring elements in a given list.
Examples:
Example1:
Input:
Given list = [5, 2, 3, 2, 3, 4, 6, 4]
Output:
The Two odd Occuring elements in a given list [5, 2, 3, 2, 3, 4, 6, 4] are : 5 6
Example2:
Input:
Given list = [2, 1, 2, 3, 1, 2, 3, 5, 5, 6]
Output:
The Two odd Occuring elements in a given list [2, 1, 2, 3, 1, 2, 3, 5, 5, 6] are : 2 6
Program to Find Two Odd Occurring Elements in an Array/List in Python
Below are the ways to find two odd occurring elements in a given list:
Method #1: Using Counter Function (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 another variable (say lstfreqelements).
- Traverse in this frequency dictionary using the for loop.
- Inside the loop, check if any Key has the value odd using the if conditional statement.
- If the statement is true, then print the key for getting the two odd occurring elements in a given list.
- 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. gvn_lst = [5, 2, 3, 2, 3, 4, 6, 4] # 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 another variable (say lstfreqelements). lstfreqelements = Counter(gvn_lst) print("The Two odd Occuring elements in a given list", gvn_lst, "are :") # Traverse in this frequency dictionary using the for loop. for key in lstfreqelements: # Inside the loop, check if any ley has the value odd using the if conditional statement. if lstfreqelements[key] % 2 != 0: # If the statement is true, then print the key for getting the two odd occurring elements # in a given list. print(key, end=" ")
Output:
The Two odd Occuring elements in a given list [5, 2, 3, 2, 3, 4, 6, 4] are : 5 6
Method #2: Using Counter Function (User Input)
Approach:
- Import the Counter() function from collections using the import keyword.
- Give the list as user input using list(),map(),input(),and split() functions.
- 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 another variable (say lstfreqelements).
- Traverse in this frequency dictionary using the for loop.
- Inside the loop, check if any key has the value odd using the if conditional statement.
- If the statement is true, then print the key for getting the two odd occurring elements in a given list.
- 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 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 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 another variable (say lstfreqelements). lstfreqelements = Counter(gvn_lst) print("The Two odd Occuring elements in a given list", gvn_lst, "are :") # Traverse in this frequency dictionary using the for loop. for key in lstfreqelements: # Inside the loop, check if any ley has the value odd using the if conditional statement. if lstfreqelements[key] % 2 != 0: # If the statement is true, then print the key for getting the two odd occurring elements # in a given list. print(key, end=" ")
Output:
Enter some random List Elements separated by spaces = 2 1 2 3 1 2 3 5 5 6 The Two odd Occuring elements in a given list [2, 1, 2, 3, 1, 2, 3, 5, 5, 6] are : 2 6
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.