How to remove duplicates from an array in Go

1 Answer

0 votes
package main
 
import "fmt"
 
func remove_duplicates_from_array(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() {
    arr := []int{1, 3, 4, 3, 3, 4, 1, 1, 5, 5, 6, 7, 8, 8, 8, 8, 9}
    
    arr = remove_duplicates_from_array(arr)
    
    fmt.Println(arr)
}



/*
run:

[1 3 4 5 6 7 8 9]

*/

 



answered Jan 27, 2025 by avibootz

Related questions

1 answer 153 views
1 answer 120 views
3 answers 126 views
126 views asked Jan 22, 2025 by avibootz
1 answer 123 views
1 answer 183 views
1 answer 181 views
...