- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Find Element Occurring Odd Number of Times in a List
When it is required to find an element that occurs odd number of times in a list, a method can be defined. This method iterates through the list and checks to see if the elements in the nested loops match. If they do, the counter is incremented. If that count is not divisible by 2, the specific element of the list is returned as the result. Otherwise, -1 is returned as the result.
Below is a demonstration of the same −
def odd_occurence(my_list, list_size): for i in range(0, list_size): count = 0 for j in range(0, list_size): if my_list[i] == my_list[j]: count+= 1 if (count % 2 != 0): return my_list[i] return -1 my_list = [34, 56, 78, 99, 23, 34, 34, 56, 78, 99, 99, 99, 99, 34, 34, 56, 56 ] print("The list is :") print(my_list) n = len(my_list) print("The length is :") print(n) print("The method to find the element that occurs odd number of times is called ") print("The element that occurs odd number of times is :") print(odd_occurence(my_list, n))
The list is : [34, 56, 78, 99, 23, 34, 34, 56, 78, 99, 99, 99, 99, 34, 34, 56, 56] The length is : 17 The method to find the element that occurs odd number of times is called The element that occurs odd number of times is : 34
A method named ‘odd_occurence’ is defined, which takes the list and its size as parameters.
The listed size is taken as the range and the list is iterated over.
Two nested loops are iterated, and if the element in the list matches the first and second loop iterations, the ‘count’ variable is incremented.
If the ‘count’ variable is an odd number, the specific element in the list is returned.
A list of integers is defined and is displayed on the console.
The length of the list is stored in a variable.
The method is called by passing relevant parameters.
The output is displayed on the console.
- Java Program to Find the Number Occurring Odd Number of Times
- C/C++ Program to Find the Number Occurring Odd Number of Times?
- C/C++ Program for Finding the Number Occurring Odd Number of Times?
- Find the Number Occurring Odd Number of Times using Lambda expression and reduce function in Python
- Golang Program to find the odd-occurring elements in a given array
- Program to find most occurring number after k increments in python
- Program to find sum of odd elements from list in Python
- Program to count number of elements in a list that contains odd number of digits in Python
- Python Program to Search the Number of Times a Particular Number Occurs in a List
- Python program to print odd numbers in a list
- Python Program for Find sum of odd factors of a number
- Python program to find largest number in a list
- Program to find replicated list by replicating each element n times
- Python Program to find the cube of each list element
- Program to find modulus of a number by concatenating n times in Python