Groovy 18 – Removing Duplicates from Array
Sometimes you may have an array with duplicate items and need to remove the duplicates to get only unique items. Here’s how you can do it in Groovy:
def myArray = [1, 20, 40, 10, 20, 40, 50, 20]
In myArray, the numbers 20 and 40 appear multiple times. Groovy has a method called unique() that removes duplicates from an array:
def myUniqueArray = myArray.unique() println(myUniqueArray)
Output:
[1, 20, 40, 10, 50]
Note that myUniqueArray is also an array.