- Related Questions & Answers
- Java Program to Find the Number Occurring Odd Number of Times
- C/C++ Program to Find the Number Occurring Odd Number of Times?
- Python program to find the number occurring odd number of times using Lambda expression and reduce function
- 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
- Python Program to Search the Number of Times a Particular Number Occurs in a List
- Program to find most occurring number after k increments in python
- Python Program for Find sum of odd factors of a number
- Program to count number of elements in a list that contains odd number of digits in Python
- Take an array and find the one element that appears an odd number of times in JavaScript
- Python program to find largest number in a list
- Python program to print odd numbers in a list
- Python program to find the largest number in a list
- Python program to find the smallest number in a list

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

Advertisements