Golang - Removes duplicates, ignores order Code Example

The code for Removes duplicates, ignores order

// Go language program to remove duplicates, ignore order

package main

import "fmt"

func uniqueValues(arr []int) []int {
	res := []int{}
	face := map[int]bool{}

	// Create a map of all uniqueValues elements.
	for v := range arr {
		face[arr[v]] = true
	}

	// Place all uniqueValues keys from
	// the map into the ress array.
	for key, _ := range face {
		res = append(res, key)
	}
	return res
}

func main() {
	array1 := []int{12, 14, 5, 7, 12, 3, 14,
			7, 7, 362, 5, 36, 36, 1, 108}
	fmt.Println(array1)
	uniqueValues_items := uniqueValues(array1)
	fmt.Println(uniqueValues_items)
}

/*
Output:
[12 14 5 7 12 3 14 7 7 362 5 36 36 1 108]
[108 12 3 362 1 14 5 7 36]
*/
Code by IncludeHelp, on March 1, 2023 07:37

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.