Xojo Array: Remove duplicate elements?

A very basic question:

Is there a Xojo Method to remove duplicate elements in xojo?

I’m searching in the Forum and the help, but didn’t find anything. Or I used the wrong search terms.

Example:
We have the array:
[1, 4, 2, 2, 6, 24, 15, 2, 60, 15, 6]

And I want to have this one:
[1, 2, 4, 6, 15, 24, 60]

The name of the array could be StringArray_Test.

So I want do:
StringArray_Test.Sort
then I have the sorting.

But now I have some double entries.

I need something like

StringArray_Test.deleteduplicates

But I can’t find the correct method. Or doesn’t Xojo have a method for this?

There’s no built in method for this. You’ll have to write your own, something like this in a module

Protected Sub SortAndDeleteDuplicates(extends arr() As String) arr.Sort for i As integer = arr.Ubound-1 downto 0 if arr(i) = arr(i+1) then arr.Remove(i) next End Sub

Use as a method on a string array variable

StringArray_Test.SortAndDeleteDuplicates

WOW! This works fantastic. THANKS!

That is what I’m searching for.

I just tried this for an array of Integers and while it works great, according to the manual, the method should be set to “Public”.

http://developer.xojo.com/extends

This makes sense, but the above does say “protected” and this is just a heads up that protected doesn’t seem to work, although it will autocomplete.

There are ways that you can do this without sorting too, if the original array order is important. Off the top of my head, create a second array of the same size as the first, then fill in the second array with unique elements from the first. Use IndexOf on the second array or an intermediate Dictionary to determine which elements to add, then resize the second array according to how many elements were actually added.

With larger arrays, I’d expect that to be faster too, but haven’t tested.