2017-06-27 11:29 GMT-03:00 Tobias Boege <tabo...@gmail.com>: > > Your first sentence is a bit confusing. First you say that your array is > sorted but then you say that duplicates may be scattered across the array. > You are right. My fault. The array is sorted. What I meant by scattered was that pairs, duples, triplets or a bunch of duplicates may appear all over interspersed with non-duplicated items.
My items are either words or sentences (extracted from an ODT file. After the extraction, the words (or sentences) are sorted with the method Array.sort(gb.descent). After sorting it is much more efficient to search for the duplicates. And it can be done with some simple code (as some people have exemplified in this thread). So, my question is basically if Gambas has some built in method do eliminate duplicates. The reason I am asking this is because I am new to Gambas, so I have found myself coding things that were not needed. For instance, I coded some functions to do quicksort and bubble sort and then I found Array.sort () was available. Therefore, I waisted my time coding those quicksort and bubble sort functions.... :-( Regards - fernando > If you have a sorting where duplicates are consecutive, the solution is > very easy: just go through the array linearly and kick out these > consecutive > duplicates (which is precisely what uniq does), e.g. for integers: > > Dim aInts As Integer[] = ... > Dim iInd, iLast As Integer > > If Not aInts.Count Then Return > iLast = aInts[0] > iInd = 1 > While iInd < aInts.Count > If aInts[iInd] = iLast Then ' consecutive duplicate > aInts.Remove(iInd, 1) > Else > iLast = aInts[iInd] > Inc iInd > Endif > Wend > > Note that the way I wrote it to get the idea across is not a linear-time > operation (it depends on the complexity of aInts.Remove()), but you can > achieve linear performance by writing better code. Think of it as an > exercise. (Of course, you can't hope to be more efficient than linear > time in a general situation.) > > The counting task is solved with a similar pattern, but while you kick > an element out, you also increment a dedicated counter: > > Dim aInts As Integer[] = ... > Dim aDups As New Integer[] > Dim iInd, iLast As Integer > > If Not aInts.Count Then Return > iLast = aInts[0] > iInd = 1 > aDups.Add(0) > While iInd < aInts.Count > If aInts[iInd] = iLast Then ' consecutive duplicate > aInts.Remove(iInd, 1) > Inc aDups[aDups.Max] > Else > iLast = aInts[iInd] > aDups.Add(0) > Inc iInd > Endif > Wend > > After this executed, the array aInts will not contain duplicates (supposing > it was sorted before) and aDups[i] will contain the number of duplicates of > the item aInts[i] that were removed. > > Regards, > Tobi > > -- > "There's an old saying: Don't change anything... ever!" -- Mr. Monk > > ------------------------------------------------------------ > ------------------ > Check out the vibrant tech community on one of the world's most > engaging tech sites, Slashdot.org! http://sdm.link/slashdot > _______________________________________________ > Gambas-user mailing list > Gambas-user@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/gambas-user > -- Fernando Cabral Blogue: http://fernandocabral.org Twitter: http://twitter.com/fjcabral e-mail: fernandojosecab...@gmail.com Facebook: f...@fcabral.com.br Telegram: +55 (37) 99988-8868 Wickr ID: fernandocabral WhatsApp: +55 (37) 99988-8868 Skype: fernandojosecabral Telefone fixo: +55 (37) 3521-2183 Telefone celular: +55 (37) 99988-8868 Enquanto houver no mundo uma só pessoa sem casa ou sem alimentos, nenhum político ou cientista poderá se gabar de nada. ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot _______________________________________________ Gambas-user mailing list Gambas-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gambas-user