s := []int{9, 8, 9, 1, 3, 2, 3, 1, 5, 2, 1, 1}
time1 := time.Now().UnixNano()
s2 := make([]int, 0)
m := make(map[int]int)
for i, v := range s {
if _, ok := m[v]; ok {
s2 = append(s2, v)
}
m[v] = i
}
//fmt.Println(len(m), m, len(s)-len(m))
s1 := make([]int, 0)
for k := range m {
s1 = append(s1, k)
}
s1 = append(s1, s2...)
fmt.Println(s1, len(s)-len(m), time.Now().UnixNano()-time1)
使用中间变量.
n := 0
p := 0
for i := 0; i < len(s); i++ {
for j := i + 1; j < len(s); j++ {
if s[i] == s[j] {
n = n + 1
p = s[j]
//删除重复元素
s = append(s[:j], s[j+1:]...)
//追加到末尾
s = append(s, p)
}
}
}
fmt.Println(s, n, time.Now().UnixNano()-time1)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)