RosettaCodeData/Task/Remove-duplicate-elements/Groovy/remove-duplicate-elements.groovy

23 lines
615 B
Groovy
Raw Permalink Normal View History

2013-04-10 23:57:08 -07:00
def list = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
assert list.size() == 12
2016-12-05 22:15:40 +01:00
println " Original List: ${list}"
2013-04-10 23:57:08 -07:00
2016-12-05 22:15:40 +01:00
// Filtering the List (non-mutating)
def list2 = list.unique(false)
assert list2.size() == 8
assert list.size() == 12
println " Filtered List: ${list2}"
// Filtering the List (in place)
2013-04-10 23:57:08 -07:00
list.unique()
assert list.size() == 8
2016-12-05 22:15:40 +01:00
println " Original List, filtered: ${list}"
2013-04-10 23:57:08 -07:00
2016-12-05 22:15:40 +01:00
def list3 = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
assert list3.size() == 12
2013-04-10 23:57:08 -07:00
// Converting to Set
2016-12-05 22:15:40 +01:00
def set = list as Set
2013-04-10 23:57:08 -07:00
assert set.size() == 8
2016-12-05 22:15:40 +01:00
println " Set: ${set}"