Logo

Programming-Idioms

History of Idiom 119 > diff from v38 to v39

Edit summary for version 39 by programming-idioms.org:
[Go] Big-O

Version 38

2018-04-11, 21:31:59

Version 39

2018-04-11, 21:32:59

Idiom #119 Deduplicate list

Remove duplicates from list x.
Explain if original order is preserved.

Illustration

Idiom #119 Deduplicate list

Remove duplicates from list x.
Explain if original order is preserved.

Illustration
Extra Keywords
deduplicate dupe dupes redundant redundancy
Extra Keywords
deduplicate dupe dupes redundant redundancy
Code
seen := make(map[T]bool)
j := 0
for _, v := range x {
	if !seen[v] {
		x[j] = v
		j++
		seen[v] = true
	}
}
x = x[:j]
Code
seen := make(map[T]bool)
j := 0
for _, v := range x {
	if !seen[v] {
		x[j] = v
		j++
		seen[v] = true
	}
}
x = x[:j]
Comments bubble
The order is preserved.
Use this if T is not a pointer type or reference type.
Comments bubble
The order is preserved.
Use this if T is not a pointer type or reference type.

This is O(n).
Demo URL
https://play.golang.org/p/yz3dDxBDTPI
Demo URL
https://play.golang.org/p/yz3dDxBDTPI