How to Remove Duplicate Values from Slice in Golang? Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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 size is not fixed. When we declare a slice we do not specify the size of it. Syntax: []mySlice; OR []mySlice{}; OR []mySlice{input1, input2, .........input n} Moreover, a slice and an array are connected with each other, in a slice, there is a referencing to an underlying array. And in a slice, we can store duplicate elements.Example: Here, we will see how to remove the duplicate elements from slice. We have defined a function where we are passing the slice values and using the map function we are checking the duplicates and removing them. C // Golang program to remove duplicate // values from Slice package main import ( "fmt" ) func removeDuplicateValues(intSlice []int) []int { keys := make(map[int]bool) list := []int{} // If the key(values of the slice) is not equal // to the already present value in new slice (list) // then we append it. else we jump on another element. for _, entry := range intSlice { if _, value := keys[entry]; !value { keys[entry] = true list = append(list, entry) } } return list } func main() { // Assigning values to the slice intSliceValues := []int{1,2,3,4,5,2,3,5,7,9,6,7} // Printing original value of slice fmt.Println(intSliceValues) // Calling function where we // are removing the duplicates removeDuplicateValuesSlice := removeDuplicateValues(intSliceValues) // Printing the filtered slice // without duplicates fmt.Println(removeDuplicateValuesSlice) } Output: [1 2 3 4 5 2 3 5 7 9 6 7] [1 2 3 4 5 7 9 6] Explanation: From the main function, we have declared a slice. Also we have print the original value of the slice.We have defined a function where we are passing the slice original values and checking the duplicates.Logic for duplicate check : For this we have defined another slice and assigning the first values by checking if the value already exists in the new slice or not. It returns the slice without duplicates.We are calling removeDuplicateValues function from main function where the return slice from the function is printed. Comment More infoAdvertise with us Next Article How to Remove Duplicate Values from Slice in Golang? P pganesh Follow Improve Article Tags : Go Language Golang-Slices Similar Reads How To Remove Duplicates From Vector In R A vector is a basic data structure that is used to represent an ordered collection of elements of the same data type. It is one-dimensional and can contain numeric, character, or logical values. It is to be noted that the vector in C++ and the vector in R Programming Language are not the same. In C+ 4 min read How to find duplicate values in a factor in R finding duplicates in data is an important step in data analysis and management to ensure data quality, accuracy, and efficiency. In this article, we will see several approaches to finding duplicate values in a factor in the R Programming Language. It can be done with two methods Using duplicated() 2 min read How to trim a slice of bytes in Golang? In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice of bytes, you ar 3 min read How to compare two slices of bytes in Golang? In Go, a slice is a powerful, flexible data structure that allows elements of the same type to be stored in a variable-length sequence. When working with byte slices ([]byte), Go provides easy-to-use functions in the bytes package to compare them. In this article we will learn "How to compare two sl 2 min read How to find the last index value in slice of bytes in Golang? In Go, a slice is a flexible data structure that stores a variable-length sequence of elements of the same type. The LastIndex() function from the bytes package returns the last index of a specified value in a byte slice, or -1 if the value is not found.Examplepackage mainimport ( "bytes" "fmt")func 3 min read How to remove duplicate characters from a String in Scala? In this article, we will learn how to remove duplicate characters from a string in Scala using different approaches. First, we will look through the Brute-Force Approach and then use different standard libraries. Examples: Input: String = "hello"Output: helo Input: String = "Geeks"Output: Geks Naive 4 min read How to print struct variables data in Golang? A struct (Structure) is a user-defined type in Golang that contains a collection of named fields/properties which creates own data types by combining one or more types. Also, this concept is generally compared with the classes in object-oriented programming. A struct has different fields of the same 2 min read How to use strconv.Quote() Function in Golang? Go language provides inbuilt support to implement conversions to and from string representations of basic data types by strconv Package. This package provides a Quote() function which is used to find a double-quoted Go string literal representing str and the returned string uses Go escape sequences 2 min read How to pass a Slice to Function in Golang? In Go, slices are dynamically sized arrays. They are often passed to functions when you want to manipulate or process collections of data. Slices are passed by reference, meaning that any modification made to the slice inside the function will affect the original slice outside the function.Examplepa 4 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 Like