Android Question How to get unique data from a list or map

Brian Michael

Member
Licensed User
Hello, best regards to all.

I am trying to obtain unique data that are in a map collection.

Example:
In a map collection I have

JSON:
[{{"Supplier": "COCA COLA", "Color": "BLACK"},
{"Supplier": "COCA COLA", "Color": "BLACK"},
{"Supplier": "COCA COLA", "Color": "BLACK"},
{"Supplier": "COCA COLA", "Color": "BLACK"},
{"Supplier": "COCA COLA", "Color": "YELLOW"},
{"Supplier": "NIKE", "Color": "ZERO"},
{"Supplier": "JORDANS", "Color": "RED"},
{"Supplier": "JORDANS", "Color": "RED"},
{"Supplier": "PEPSI", "Color": "NORMAL"},
{"Supplier": "PEPSI", "Color": "LITE"},
{"Supplier": "PEPSI", "Color": "LITE"},
{"Supplier": "PEPSI", "Color": "LITE"},
}]

So I would like to get only:

Supplier: COCA COLA, Color: BLACK
Supplier: COCA COLA, Color: RED
Supplier: COCA COLA, Color: YELLOW
Supplier: NIKE, Color: ZERO
Supplier: JORDANS, Color: RED
Supplier: PEPSI, Color: NORMAL
Supplier: PEPSI, Color: LITE

I would appreciate a lot if you can help me with this question.
 

udg

Expert
Licensed User
Longtime User
Hi, are you sure you have a regular JSON input?
Shouldn't it be something like:
B4X:
[{"Supplier": "COCA COLA", "Color": "BLACK"},
{"Supplier": "COCA COLA", "Color": "BLACK"},
{"Supplier": "COCA COLA", "Color": "BLACK"},
{"Supplier": "COCA COLA", "Color": "BLACK"},
{"Supplier": "COCA COLA", "Color": "YELLOW"},
{"Supplier": "NIKE", "Color": "ZERO"},
{"Supplier": "JORDANS", "Color": "RED"},
{"Supplier": "JORDANS", "Color": "RED"},
{"Supplier": "PEPSI", "Color": "NORMAL"},
{"Supplier": "PEPSI", "Color": "LITE"},
{"Supplier": "PEPSI", "Color": "LITE"},
{"Supplier": "PEPSI", "Color": "LITE"}
]
 
Upvote 0

Brian Michael

Member
Licensed User
Hi, are you sure you have a regular JSON input?
Shouldn't it be something like:
B4X:
[{"Supplier": "COCA COLA", "Color": "BLACK"},
{"Supplier": "COCA COLA", "Color": "BLACK"},
{"Supplier": "COCA COLA", "Color": "BLACK"},
{"Supplier": "COCA COLA", "Color": "BLACK"},
{"Supplier": "COCA COLA", "Color": "YELLOW"},
{"Supplier": "NIKE", "Color": "ZERO"},
{"Supplier": "JORDANS", "Color": "RED"},
{"Supplier": "JORDANS", "Color": "RED"},
{"Supplier": "PEPSI", "Color": "NORMAL"},
{"Supplier": "PEPSI", "Color": "LITE"},
{"Supplier": "PEPSI", "Color": "LITE"},
{"Supplier": "PEPSI", "Color": "LITE"}
]


Hi @udg thanks for answering, just doing an example with that code, what I need is to get only the unique Suppliers and Colors
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
If these data come from a query of yours, you can apply a distinct containing supplier and color:

B4X:
select distinct supplier,color from yourTable

If not, you can fetch your data into a list, after checking that there's no previous identical one.
Pseudocode:
B4X:
private newList as list
newList.initialize
for each element in yourOriginalList
     if newList.indexOf(element)=-1 then 
          newList.add(element)
     end if
next
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4J example:
B4X:
Sub Process_Globals
    Type SCItem (Supplier As String, Clr As String)    
End Sub

Sub AppStart (Args() As String)
    Dim s As String = $"[{"Supplier": "COCA COLA", "Color": "BLACK"},
{"Supplier": "COCA COLA", "Color": "BLACK"},
{"Supplier": "COCA COLA", "Color": "BLACK"},
{"Supplier": "COCA COLA", "Color": "BLACK"},
{"Supplier": "COCA COLA", "Color": "YELLOW"},
{"Supplier": "NIKE", "Color": "ZERO"},
{"Supplier": "JORDANS", "Color": "RED"},
{"Supplier": "JORDANS", "Color": "RED"},
{"Supplier": "PEPSI", "Color": "NORMAL"},
{"Supplier": "PEPSI", "Color": "LITE"},
{"Supplier": "PEPSI", "Color": "LITE"},
{"Supplier": "PEPSI", "Color": "LITE"}
]"$
    Dim items As Map
    items.Initialize
    Dim parser As JSONParser
    parser.Initialize(s)
    For Each m As Map In parser.NextArray
        Dim item As SCItem = CreateSCItem(m.Get("Supplier"), m.Get("Color"))
        items.Put(SCItemToKey(item), item)
    Next
    
    For Each item As SCItem In items.Values
        Log(item)
    Next
End Sub

Private Sub SCItemToKey (item As SCItem) As String
    Return item.Supplier & ":" & item.Clr
End Sub


Public Sub CreateSCItem (Supplier As String, Clr As String) As SCItem
    Dim t1 As SCItem
    t1.Initialize
    t1.Supplier = Supplier
    t1.Clr = Clr
    Return t1
End Sub
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi @Erel ,
yesterday I tried with regex but failed. Best achievement was to "extract" the duplicates (so, what is supposed to be discarded..).
I couldn't find a "negation symbol", something that tells "everything but what is found by the following criteria".
Anyway, it was just an exercise for a boring Saturday afternoon.. :)

ps: I'm addressing you since in the past you proved to be a master with regex. Anybody else is more than welcomed to suggest a regex-solution.
 
Upvote 0
Top