- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
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
Golang Program to find the odd-occurring elements in a given array
Examples
For example, arr = [1, 4, 5, 1, 4, 5, 1] => Odd-occurring element in the array is: 1
Approach to solve this problem
Step 1 − Define method that accepts an array.
Step 2 − Declare a xor variable, i.e., xor := 0.
Step 3 − Iterate input array and perform xor operation with each element of the array.
Step 4 − At the end, return xor.
Example
package main import ( "fmt" ) func FindOddOccurringElement(arr []int) int{ xor := 0 for i := 0; i < len(arr); i++ { xor = xor ^ arr[i] } return xor } func main(){ arr := []int{1, 4, 5, 1, 4, 5, 1} fmt.Printf("Input array is: %d\n", arr) fmt.Printf("Odd occurring element in given array is: %d\n", FindOddOccurringElement(arr)) }
Output
Input array is: [1 4 5 1 4 5 1] Odd occurring element in given array is: 1
- Related Articles
- Write a Golang program to find duplicate elements in a given array
- Golang Program To Find Common Array Elements
- Write a Golang program to calculate the sum of elements in a given array
- Java Program to Find the Number Occurring Odd Number of Times
- Write a Golang program to find duplicate elements in a given range
- Python Program to Find Element Occurring Odd Number of Times in a List
- Golang Program to Print Odd Numbers Within a Given Range
- C/C++ Program to Find the Number Occurring Odd Number of Times?
- Golang Program to get array elements after N elements
- C/C++ Program to find the sum of elements in a given array
- Program to find sum of elements in a given array in C++
- Golang program to shuffle the elements of an array
- Golang Program to Determine Recursively Whether a Given Number is Even or Odd
- Golang Program to cyclically permutes the elements of the array
- Golang Program to remove given element from the array

Advertisements