Sobes.tech
Senior

In: [1, 2, 3, 4, 5] Out: True In: [1, 2, 3, 3, 4, 5, 1] Out: False

sobes.tech AI

Answer from AI

To check if all elements in a slice are unique, you can use a map to track already encountered values. If an element is already in the map, it means there is a duplicate — return false, otherwise true.

Example in Go:

func allUnique(nums []int) bool {
    seen := make(map[int]bool)
    for _, num := range nums {
        if seen[num] {
            return false
        }
        seen[num] = true
    }
    return true
}

// Usage:
// fmt.Println(allUnique([]int{1, 2, 3, 4, 5})) // true
// fmt.Println(allUnique([]int{1, 2, 3, 3, 4, 5, 1})) // false
In: [1, 2, 3, 4, 5] Out: True In: [1, 2, 3, 3, 4, 5… - sobes.tech