Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Find the number which appears once in an array in Go(Golang)

Posted on July 14, 2023July 14, 2023 by admin

Table of Contents

  • Overview
  • Program

Overview

An array is given in which every element is present twice except one element. The objective is to find that element in constant extra space

Example 1

Input: [2, 1, 2, 3, 3]
Output: 1

Example 2

Input: [1, 1, 4]
Output: 4

The idea is to use XOR here. Here we will use two properties of XOR

  • XOR of a number with itself is 0
  • XOR of 0 and any number is that number

So the idea is to do an XOR of all the numbers in the array. The number that we obtain at the end will be the answer.

Program

Below is the program for the same

package main
import "fmt"
func singleNumber(nums []int) int {
lenNums := len(nums)
res := 0
for i := 0; i < lenNums; i++ {
res = res ^ nums[i]
}
return res
}
func main() {
output := singleNumber([]int{2, 1, 2, 3, 3})
fmt.Println(output)
output = singleNumber([]int{1, 1, 4})
fmt.Println(output)
}

Output:

1
4

Note: Check out our Golang Advanced Tutorial. The tutorials in this series are elaborative and we have tried to cover all concepts with examples. This tutorial is for those who are looking to gain expertise and a solid understanding of golang - Golang Advance Tutorial

Also if you are interested in understanding how all design patterns can be implemented in Golang. If yes, then this post is for you - All Design Patterns Golang

Also, check out our system design tutorial series here - System Design Tutorial Series

Popular Articles

Golang Comprehensive Tutorial Series

All Design Patterns in Go (Golang)

Slice in golang

Variables in Go (Golang) – Complete Guide

OOP: Inheritance in GOLANG complete guide

Using Context Package in GO (Golang) – Complete Guide

All data types in Golang with examples

Understanding time and date in Go (Golang) – Complete Guide

©2023 Welcome To Golang By Example | Design: Web XP