golang顺序切片查重

切片查重

Posted by 果果 on December 2, 2022

golang顺序切片查重

package main

import "fmt"

func unicorn(s []int) int {
	if len(s) == 1 {
		return s[0]
	}

	if len(s) == 2 {
		if s[0] == s[1] {
			return 0
		}
		return s[0]
	}

	for i, j := 0, 1; j < len(s); i, j = i+2, j+2 {
		if s[i] != s[j] {
			return s[i]
		}
	}

	return 0
}

func main() {
	s1 := []int{1, 1, 2, 2, 3, 4, 4, 5, 5, 6, 6, 7, 8, 8, 9, 9, 10, 10}
	i := unicorn(s1)
	fmt.Println(i)
}