[Gambas-user] To the wish list...

Benoît Minisini gambas at ...1...
Sat Aug 22 11:52:08 CEST 2009


> Jussi Lahtinen ha scritto:
> > Meaning like this? Should be faster!
> >
> > Dim col As New Collection
> >
> > For ii = 0 To iMyArray.Max
> > col.Add(iMyArray[ii],  iMyArray[ii])
> > Next
> >
> > iMyArray.Clear()
> >
> > For Each ii In col
> > iMyArray.Add(ii)
> > Next
> >
> >
> > Jussi
> >
> > 2009/8/21 Benoît Minisini <gambas at ...1...>:
> >>> Now I use algorithm like this (maybe not optimal);
> >>>
> >>> Dim ii As Integer
> >>> Dim jj As Integer
> >>> Dim bRemove As Boolean
> >>>
> >>> While ii < iMyArray.Count
> >>>
> >>>  jj = 0
> >>>  bRemove = False
> >>>  While jj < iMyArray.Count
> >>>   If ii <> jj And iMyArray[ii] = iMyArray[jj] Then
> >>>   iMyArray.Remove(ii)
> >>>   bRemove = True
> >>>   Else
> >>>   Inc jj
> >>>   Endif
> >>>  Wend
> >>>
> >>> If bRemove = False Then
> >>> Inc ii
> >>> Endif
> >>> Wend
> >>>
> >>>
> >>> Good point Benôit. Array is populated from file, but maybe I should use
> >>> .Find() before .Add().
> >>>
> >>>
> >>> Jussi
> >>
> >> You should use a collection to detect duplicates, otherwise your
> >> algorithm is O(n^2)
>
> Another technique could be to keep the array (or collection, don't know)
> sorted while constructing it. If sorted, a single run can detect all the
> duplicates, giving O(n). If the logic behind requires the values in the
> original order, then this information should be kept in a parallel array.
>
> Just for fun...
>
> Regards,
> Doriano
>

Yes. I wanted to add two methods: AddSorted() and FindSorted(), that will 
assume the array is sorted, and so will be fastest.

Regards,

-- 
Benoît




More information about the User mailing list