Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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
Advertisements
