How can I count only unique values in a list?

I see that there isn't a clear way of retrieving a unique items list and counting those values. What other options are there? For example I have the following list of values {A, B, C, D, A} and I want to return a count of 4 - is there a way to do that with microflows?
5 answers

Create the list and add the objects to the list. Then do a list operation where you intersect the list with itself. Then you have all the unique items. Then just aggregate the list with a count action. It is that simple or am I missing something?




Alternatively if you want to make a distinction based on a certain attribute value:

  1. Create new list (uniqueValueList)
  2. Iterate through your (notOnlyUniqueValueList) list and if the "Find" [whatever attribute is your criteria] operation in uniqueValueList returns nothing, add current item to (uniqueValueList) list.

There are no lists of primitive values so I'm not sure what you're asking. Is this about filtering on a certain attribute of an entity? In that case there is no straightforward solution for this that doesn't involve Java code.

That said, if you want to filter unique OBJECTS in a list (so your A, B, C are actually instances of entities) then you can simply use the Union or Intersect list operations while inputting the same list for both of the parameters. These are set operations and will result in a list of unique objects.



You could sort the list (on an attribute) and iterate through the list. Create a counter variable that you increase every time that the iterator value (of the sorted attribute) != to the iterator value -1 (the previous value, stored in a variable). Or as stated use java and a HashMap, iterate through the arraylist and add the value to the HashMap, this will result in a list of unique values and then you only need the size of the HashMap to know the distinct number of variables in the list.


If you just want to get all unique object in a list, you can do a union with the same list as input. The result would be a list of unique object.

For instance: if your list contains objects (A,B,C,C,C,D,D,E,E), after the union your new list will be (A,B,C,D,E). You can do a simple count on your new list and voila. Simple as that.