Android Question How to check duplicates in array

Discussion in 'Android Questions' started by Germán Arduino, Jan 16, 2018.

  1. Germán Arduino

    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.
     
  2. Lee Gillie CCP

    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.
     
    LucaMs likes this.
  3. Germán Arduino

    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.
     
  4. jahswani

    jahswani Active Member Licensed User

    Look at this.
     
    LucaMs and Lee Gillie CCP like this.
  5. Peter Simpson

    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.
    Code:
    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: Jan 16, 2018
    jahswani likes this.
  6. Germán Arduino

    Germán Arduino Member Licensed User

    Thanks Peter, I understand the sense of the example, I will try with my application.

    Cheers.
     
  7. udg

    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.
     
    LucaMs, fredo, Erel and 2 others like this.
  8. Germán Arduino

    Germán Arduino Member Licensed User

    Thanks udg, I stiil must read about map.

    Cheers.
     
  9. Jeffrey Cameron

    Jeffrey Cameron Active Member Licensed User

  10. Germán Arduino

    Germán Arduino Member Licensed User

    Thanks you Jeffrey!
     
  11. Erel

    Erel Administrator Staff Member Licensed User

  12. Germán Arduino

    Germán Arduino Member Licensed User

    Thanks you Erel, I will check!
     
  13. Germán Arduino

    Germán Arduino Member 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, for example, I see

    Code:
    If days.ContainsKey(8Then ...
    But do not exist some sort of "ContainsValue" or similar to check if some value exist in any key?
     
    Last edited: Jan 18, 2018
  14. Germán Arduino

    Germán Arduino Member Licensed User

    I did it with the basics of your example! Thanks you very much!!!
     
    Peter Simpson likes this.
  15. udg

    udg 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).
     
    Peter Simpson likes this.
  16. Peter Simpson

    Peter Simpson Expert Licensed User

    UDG is saying to just do the following instead, personally it never actually crossed my mind ;)

    Code:
    '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(099), "")
        
    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: Jan 18, 2018
  17. Mahares

    Mahares Well Known Member Licensed User

    The only thing I would change is this line. It should be:
    Mappy.Put(Rnd(0, 100), ""). Otherwise, you are excluding 99 as a key
     
    Peter Simpson and Erel like this.
  18. Germán Arduino

    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:

    Code:
    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(0046)
            
    Do While MyList.IndexOf(ArrayItem) <> -1
                 ArrayItem = 
    Rnd(0046)
            
    Loop
            MyList.InsertAt(i, ArrayItem)
         
    Next
    Seems to work!
     
  19. Mahares

    Mahares Well Known Member Licensed User

    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:
    Code:
    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(0046)
            
    Do While MyList.IndexOf(ArrayItem) <> -1
                ArrayItem = 
    Rnd(0046)
            
    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
     
  20. Germán Arduino

    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!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice