Sunday challenge.
How can I create an ARRAY with unique value. In other langage I will use a SET. Of course I already have some solution but I’m looking for an elegant’s one (we are Sunday)
Sunday challenge.
How can I create an ARRAY with unique value. In other langage I will use a SET. Of course I already have some solution but I’m looking for an elegant’s one (we are Sunday)
I’d use a dictionary. You can iterate over the entries when you need to.
some of your solution is to use the Extends method.
For concrete example imagine an array of color with unique colors
// Pseudo Code
Var colors() As Color
colors.Add(Color.Black)
colors.Add(Color.Red)
colors.Add(Color.Blue)
colors.Add(Color.Black)
System.DebugLog(colors.Count)
Will produce this
3
I expect to have 3 colors in my colors array : black red blue because the colors.Add(Color.Black) will not add the existing black color.
@MarkusR is it possible to Extends array add method ? may be by creating a Set class withe super Class has Array ?
An other application of a Set of unique value in array is retrieving letters in a string with Split
For ex. “dana brown”.Split return [“d”,“a”,“n”,“a”," “,“b”,“r”,“o”,“w”,“n”] with Set it would return [“d”,“a”,“n”,” ", “b”,“r”,“o”,“w”]
Hello @Dana_Brown
Sub Pressed() Handles Pressed
Var colors() As Color
Call colors.AddUnique(Color.Black)
Call colors.AddUnique(Color.Red)
Call colors.AddUnique(Color.Blue)
Call colors.AddUnique(Color.Black)
System.DebugLog(colors.Count.ToString)
End Sub
Global Method in a Module
this Extends is a nice feature in Xojo.
Public Function AddUnique(Extends allColors() As Color, newColor As Color) As Boolean
For Each col As Color In allColors
If col = newColor Then Return False
Next
Call allColors.Add(newColor)
Return True
End Function
may be by creating a Set class withe super Class has Array
you can not use Array as Super in your new Class.
but you can have a array in your class, sure.