powershellset

Set (data structure) in PowerShell


Is there a way the define a Set data-structure in PowerShell?

In computer science, a set is an abstract data type that can store certain values, without any particular order, and no repeated values. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set.

I need to use a data structure as keystore that:


Solution

  • You can use the .NET HashSet class that is found under System.Collections.Generic:

    $set = New-Object System.Collections.Generic.HashSet[int]
    

    The collection guarantees unique items and the Add, Remove, and Contains methods all operate with O(1) complexity on average.