1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| package main
import ( "fmt" "sort" )
func RemoveDuplicate(v []int) []int { toIndex := 0 p := 0
for i := range v { c := &v[i]
if p == *c && i != 0 { continue }
if i != toIndex { v[toIndex] = *c }
toIndex++ p = *c }
return v[:toIndex] }
func main() { v := []int{9, 1, 1, 9, 2, 2, 3, 3, 3, 4, 5, 6, 7, 7, 7, 7, 8}
sort.Slice(v, func(i, j int) bool { return v[i] <= v[j] })
v = RemoveDuplicate(v)
fmt.Printf("%#v", v) }
func ArrayRemoveRepeated(arr []string) []string { sort.Strings(arr) i := 0 var j int for { if i >= len(arr)-1 { break } for j = i + 1; j < len(arr) && arr[i] == arr[j]; j++ { } arr = append(arr[:i+1], arr[j:]...) i++ } return arr }
|