Golang Program that Removes Duplicate Elements From the Array
Last Updated :
19 Sep, 2021
Arrays in Golang or Go programming language is much similar to other programming languages. In the program, sometimes we need to store a collection of data of the same type, like a list of student marks. Such type of collection is stored in a program using an Array. An array is a fixed-length sequence that is used to store homogeneous elements in the memory. Due to their fixed length array are not much popular like Slice in Go language.
In an array, you are allowed to store zero or more than zero elements in it. The elements of the array are indexed by using the [] index operator with their zero-based position, means the index of the first element is array[0] and the index of the last element is array[len(array)-1].
To remove duplicate elements from an array we map the int items to boolean data type and set it to true for the first occurrence of the elements and thereby append it to another array to get the removed duplicated elements array
Input : [7, 3, 6, 1, 8, 1, 1, 3, 4, 5, 12]
Output : [7, 6, 4, 8, 5, 12]
The elements present more than once, the duplicates are removed
Lets look into the Golang program and understand the code :
import "fmt"
Package fmt implements formatted I/O with functions analogous to C’s printf and scanf.
func unique(arr []int) []int {
The function definition that we define to remove duplicate elements with the parameter as an input array ‘arr’ and return an array of type ‘[ ]int’

occurred := map[int]bool{}
result:=[]int{}
Here we create a map variable occurred that will map int data type to boolean data type for every element present in the array. We create and array ‘result’ that will store the unique elements from the given items and return it to the main function
for e:= range arr {
if occurred[arr[e]] != true {
occurred[arr[e]] = true
result = append(result, arr[e])
}
}
return result
We iterate through the array in for loop and check if the mapped variable occurred[arr[(loop variable)]] is true or not. Initially none of the mapped variable for each index is true. So when the element is encountered first time we change the value to true and append it to an array result. The next time when the duplicate elements are found it will check for the condition if occurred[arr[e]] is true or not, it will be true as it was encountered earlier so the block is not executed and the duplicate elements does not get appended to the result array. Finally we return the array
func main() {
array1 := []int{1, 5, 3, 4, 1, 6, 6, 6, 8, 7, 13, 5}
fmt.Println(array1)
unique_items := unique(array1)
fmt.Println(unique_items)
}
In the function main array1 is initialized with the values then we print the initial array and call the function unique to execute the above function and in turn returns an array which we print it as ‘unique_items’
Go
package main
import "fmt"
func unique(arr []int) []int {
occurred := map [int]bool{}
result := []int{}
for e := range arr {
if occurred[arr[e]] != true {
occurred[arr[e]] = true
result = append(result, arr[e])
}
}
return result
}
func main() {
array1 := []int{ 1 , 5 , 3 , 4 , 1 , 6 , 6 , 6 , 8 , 7 , 13 , 5 }
fmt.Println(array1)
unique_items := unique(array1)
fmt.Println(unique_items)
}
|
Output:
[1 5 3 4 1 6 6 6 8 7 13 5]
[1 5 3 4 6 8 7 13]
Similar Reads
How to Remove Duplicate Elements from NumPy Array
In this article, we will see how to remove duplicate elements from NumPy Array. Here we will learn how to Remove Duplicate Elements from a 1-D NumPy Array and 2-D NumPy Array. Input1: [1 2 3 4 5 1 2 3 1 2 9]Output1: [1 2 3 4 5 9]Explanation: In this example, we have removed duplicate elements from o
7 min read
How to Remove Duplicate Elements from Array in Ruby?
This article focuses on discussing how to remove duplicate elements from an array in Ruby. In Ruby, an array is a fundamental data structure that stores a collection of elements in a specific order. Various methods and approaches can be used to remove duplicate elements from an array. Removing Dupli
2 min read
How to Remove duplicate elements from array in JavaScript ?
In this article, we will discuss the methods to remove duplicate elements from a Javascript array. There are various methods to remove duplicates in the array. These are the following ways: Table of Content Using filter() MethodUsing set() MethodUsing forEach() MethodUsing reduce() MethodUsing index
4 min read
How to Remove Duplicate Elements from an Array using Lodash ?
Removing duplicate elements from an array is necessary for data integrity and efficient processing. The approaches implemented and explained below will use the Lodash to remove duplicate elements from an array. Table of Content Using uniq methodUsing groupBy and map methodsUsing xor functionUsing un
3 min read
Remove Duplicate Elements from JavaScript Array
To Remove the elements from an array we can use the JavaScript set method. Removing duplicate elements requires checking if the element is present more than one time in the array. 1. Using JavaScript Set() - Mostly UsedThe JavaScript Set() method creates an object containing only unique values. To r
3 min read
Golang program that removes duplicates, ignores order
In Golang when we want to remove the duplicates not considering any particular order as the initial values, we make use of Mapping in Go lang.Firstly iterate through the loop and map each and every element in the array to boolean data type. Go provides a built-in map type that implements a hash tabl
3 min read
Golang Program that Removes Duplicates Using Nested Loops
Given an array of size n. Your task is to remove the duplicates from the array. Examples: Input : array: 1 2 1 2 1 3 2 Output :1 2 3 Input :array: 10 24 5 10 24 Output :10 24 5 We will use two loops to solve this problem. The first loop i will traverse from 0 to the length of the array. The second l
2 min read
Golang Program to Show the Duplicate Case Error in Switch Statement
A switch's case must have a unique value. When repeated value is checked against the entire switch. The "duplicate case" error is arised. Let us discuss duplicate case error with the help of the examples: Example 1: Â [GFGTABS] Go // Golang program that causes // duplicate case error package main im
1 min read
How to Remove Duplicates From Array Using VBA in Excel?
Excel VBA code to remove duplicates from a given range of cells. In the below data set we have given a list of 15 numbers in âColumn Aâ range A1:A15. Need to remove duplicates and place unique numbers in column B. Sample Data: Cells A1:A15 Final Output: VBA Code to remove duplicates and place into n
2 min read
How to Remove Duplicate Values from Slice in Golang?
An array is a data structure. Similarly, in Golang we have slice which is more flexible, powerful, lightweight and convenient than array. As slice is more flexible than array therefore, its flexibility is determined in terms of its size. Just like an array, it has indexing value and length but its s
2 min read