mirror of
https://github.com/golang/go.git
synced 2025-05-05 15:43:04 +00:00
add in place deduplicate trick
parent
1c58cd10dc
commit
b9434c8435
@ -165,4 +165,22 @@ batches = append(batches, actions)
|
|||||||
Yields the following:
|
Yields the following:
|
||||||
```go
|
```go
|
||||||
[[0 1 2] [3 4 5] [6 7 8] [9]]
|
[[0 1 2] [3 4 5] [6 7 8] [9]]
|
||||||
|
```
|
||||||
|
|
||||||
|
### In-place deduplicate (comparable)
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "sort"
|
||||||
|
|
||||||
|
in := []int{3,2,1,4,3,2,1,4,1} // any item can be sorted
|
||||||
|
sort.Ints(in)
|
||||||
|
j := 0
|
||||||
|
for i := 1; i < len(in); i++ {
|
||||||
|
if in[j] == in[i] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
j++
|
||||||
|
in[i], in[j] = in[j], in[i]
|
||||||
|
}
|
||||||
|
fmt.Println(in[:j+1]) // [1 2 3 4]
|
||||||
```
|
```
|
Loading…
x
Reference in New Issue
Block a user