Logo

Programming-Idioms

History of Idiom 119 > diff from v36 to v37

Edit summary for version 37 by programming-idioms.org:
New Go implementation by user [programming-idioms.org]

Version 36

2018-04-11, 19:45:00

Version 37

2018-04-11, 19:49: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]
Comments bubble
The order is preserved.
Use this if T is not a pointer type or reference type.
Demo URL
https://play.golang.org/p/yz3dDxBDTPI