go 方法数组去重

package main 

import(“fmt”
“strings”

)



func main(){
     arr :=make([]string,0)
	x:=[]string{"1","3","1"}
	arr =append(arr,x...)
	fmt.Println(arr)
	fmt.Println(RemoveRepeatedElementAndEmpty(arr))
}


func RemoveRepeatedElementAndEmpty(arr []string) []string {
	newArr := make([]string, 0)
	for _, item := range arr {
		if "" == strings.TrimSpace(item) {
			continue
		}

		repeat := false
		if len(newArr) > 0 {
			for _, v := range newArr {
				if v == item {
					repeat = true
					break
				}
			}
		}

		if repeat {
			continue
		}

		newArr = append(newArr, item)
	}
	return newArr
}

  

 

 去除数组中重复的元素

posted on 2022-03-12 09:50  kevin_yang123  阅读(1045)  评论(0编辑  收藏  举报