Android Question How to check duplicates in array

Germán Arduino

Member
Licensed User
Hi Guys:

I must calculate 10 random numbers between 00 and 99 and populate an array with such ten numbers, but I need to take care that the final array do not contains duplicates values. Each element of the array must be an unique value between 00 and 99.

Exist some function (as in other languages) to test duplicates in an array?

Thanks for your comments.

Cheers.
 

Lee Gillie CCP

Active Member
Licensed User
Why not use a List rather than array? Then you can check to see if it already contains your prospective new addition with INDEXOF.
 

Germán Arduino

Member
Licensed User
Thanks for your answer!

I not considered a List, simply because I'm new with B4 family of tools.

Can you point some example or documentation?

Thanks.
 

Peter Simpson

Expert
Licensed User
NOT TESTED BUT SHOULD WORK.

Use RND in a loop to generate 10 unique numbers and add one number at a time to the list, use with IndexOf to make sure that the generated number is not already in the list. I've not added the loop but it will work. I'm not currently on my laptop, I'm typing on a tablet right now.
B4X:
Globals_Sub
    Private MyList As List

Activity_Sub 'Just for testing
    MyList.Initialize
    MyList.AddAll(Array As Int(0,1,2,4,5,6,7,8,9)) 'Adds 0,1,2,4,5,6,7,8,9 to list

    Dim ArrayItem As Int = 3 'Item to be added to list
    Log(MyList.IndexOf(ArrayItem)) 'See if 3 is already in the list (returns item index or -1)

    If MyList.IndexOf(ArrayItem) = -1 Then MyList.Add(ArrayItem) '-1 means not in list, Add 3 to list

    For i = 0 To MyList.Size - 1
        Log(MyList.Get(i)) 'Show the list in the logs (3 should be at the bottom of the list, hopefully)
    Next
Enjoy...
 
Last edited:

udg

Expert
Licensed User
Another option: use a map where the extracted number is the key. Once your map's size reaches 10 know you have 10 different values and you can fill the array.

This is based on the fact that a map doesn't allow duplicates.
 

Germán Arduino

Member
Licensed User
Last edited:

Germán Arduino

Member
Licensed User
NOT TESTED BUT SHOULD WORK.

Use RND in a loop to generate 10 unique numbers and add one number at a time to the list, use with IndexOf to make sure that the generated number is not already in the list. I've not added the loop but it will work. I'm not currently on my laptop, I'm typing on a tablet right now.
B4X:
Globals_Sub
    Private MyList As List

Activity_Sub 'Just for testing
    MyList.Initialize
    MyList.AddAll(Array As Int(0,1,2,4,5,6,7,8,9)) 'Adds 0,1,2,4,5,6,7,8,9 to list

    Dim ArrayItem As Int = 3 'Item to be added to list
    Log(MyList.IndexOf(ArrayItem)) 'See if 3 is already in the list (returns item index or -1)

    If MyList.IndexOf(ArrayItem) = -1 Then MyList.Add(ArrayItem) '-1 means not in list, Add 3 to list

    For i = 0 To MyList.Size - 1
        Log(MyList.Get(i)) 'Show the list in the logs (3 should be at the bottom of the list, hopefully)
    Next
Enjoy...
I did it with the basics of your example! Thanks you very much!!!
 

udg

Expert
Licensed User
I did a quick look at the tutorial, and tried some code, but I do not find a way to test if some element is in the map
The idea was to avoid the test since a map doesn't allow duplicate keys.
So, when you extract again a random value already used, this one will take the place of itself in the map. You exit the extraction loop when your map has 10 items (i.e. 10 different rnd numbers).
 

Peter Simpson

Expert
Licensed User
The idea was to avoid the test since a map doesn't allow duplicate keys.
So, when you extract again a random value already used, this one will take the place of itself in the map. You exit the extraction loop when your map has 10 items (i.e. 10 different rnd numbers).
UDG is saying to just do the following instead, personally it never actually crossed my mind ;)

B4X:
'Declare your map and Initialize it
    Dim Mappy As Map
        Mappy.Initialize

'Populate the map with 10 unique random numbers
    Do Until Mappy.Size = 10
        Mappy.Put(Rnd(0, 99), "")
    Loop

'Read the 10 unique numbers out of the map into the logs
    For Each Key As Int In Mappy.Keys
        Log(Key)
    Next
Enjoy...
 
Last edited:

Germán Arduino

Member
Licensed User
Ahhh, you means using the keys (no the values) of the map as the random! never crossed my mind neither.

I finally did it at this way:

B4X:
MyList.Initialize
   
    ' I need 6 different numbers between 0 and 45
    MyList.AddAll(Array As Int(46,46,46,46,46,46))
   
    For i = 1 To 6
        Dim ArrayItem As Int = Rnd(00, 46)
        Do While MyList.IndexOf(ArrayItem) <> -1
             ArrayItem = Rnd(00, 46)
        Loop
        MyList.InsertAt(i, ArrayItem)
     Next
Seems to work!
 

Mahares

Well Known Member
Licensed User
Seems to work!
Although using a map is the preferred way as Peter pointed it out with a clear example, I am afraid your code is not correct:. If you are intent in using a list and your code, you should add a second list. See example below:
B4X:
Dim MyList,MyList2 As List
    MyList.Initialize
    MyList2.Initialize
    MyList.AddAll(Array As Int(46,46,46,46,46,46)) 
    For i = 1 To 6
        Dim ArrayItem As Int = Rnd(00, 46)
        Do While MyList.IndexOf(ArrayItem) <> -1
            ArrayItem = Rnd(00, 46)
        Loop
        MyList.InsertAt(i, ArrayItem)
        MyList2.Add(MyList.Get(i))  'YOU NEED TO ADD THIS LINE
    Next
    Log(MyList2)  'displays 6 random non duplicate numbers
 

Germán Arduino

Member
Licensed User
Thanks for your comment Mahares.

mmm, you are right (I must still understand how work the lists) but I do not noticed the problem because I extracted (in my code) the elements 1 to 6 of the resulting list.

Thanks for your clarification!
 
Top