- 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
Write a Golang program to find duplicate elements in a given range
We can solve this problem in two different ways. Let’s check the first method.
Method 1:
Examples
Input Array = [1, 2, 3, 4, 4] => Range is from 1 to 5 but 4 is a duplicate element in this range.
Approach to solve this problem
- Step 1: Define a method that accepts an array.
- Step 2: Declare a visited map.
- Step 3: Iterate the given array. If the element exists in the visited map, then return that element.
- Step 4: Else, return -1.
Program
package main import "fmt" func duplicateInArray(arr []int) int{ visited := make(map[int]bool, 0) for i:=0; i<len(arr); i++{ if visited[arr[i]] == true{ return arr[i] } else { visited[arr[i]] = true } } return -1 } func main(){ fmt.Println(duplicateInArray([]int{1, 2, 3, 4, 4})) fmt.Println(duplicateInArray([]int{4, 5, 6, 7, 7})) fmt.Println(duplicateInArray([]int{1, 2, 3, 4, 5})) }
Output
4 7 -1
Now, let’s check the second method to solve this problem.
Method 2: Using XOR operation
Examples
Input Array = [1, 2, 3, 4, 4] => Range is from 1 to 5 but 4 is duplicate in that range.
Range is from 1 to 5. => XOR => 0^1^2^3^4^4^0^1^2^3^4 => 4 (since 0^1=1).
Approach to solve this problem
- Step 1: Define a method that accepts an array.
- Step 2: Find the range value from the given array and define a variable xor, initialize with 0.
- Step 3: Iterate the given array and do a xor operation with the array’s elements.
- Step 4: Also perform xor operation from the lower range value to the higher range value.
- Step 5: At the end, return the xor variable, non-zero value for duplicate element.
Program
package main import "fmt" func duplicateInArray(arr []int, r int) int{ xor := 0 for i:=0; i<len(arr); i++{ xor ^= arr[i] } for j:=1; j<=r-1; j++{ xor ^= j } return xor }
Output
4 3 1 0
- Related Articles
- Write a Golang program to find duplicate elements in a given array
- Write a Golang program to find prime numbers in a given range
- Write a Golang program to calculate the sum of elements in a given array
- Golang Program to find the odd-occurring elements in a given array
- Golang Program to Print Odd Numbers Within a Given Range
- Program to update elements in a given range in Python
- Golang Program To Remove Duplicate Elements From An Array
- Write a Golang program to find the factorial of a given number (Using Recursion)
- Write a Golang program to find the sum of digits for a given number
- Golang Program to Print all Numbers in a Range Divisible by a Given Number
- Write a program in Python to remove first duplicate rows in a given dataframe
- Write a Golang program to find pairs with given sum in an array(O(nlogn))
- Write Python program to find duplicate rows in a binary matrix
- Write a program in Python to check if a series contains duplicate elements or not
- Write a program in Python to print the elements in a series between a specific range

Advertisements