Count the occurrence of an unknown element in an array

Overview: As part of implementing a client use-case, I have a requirement to fetch intersection meeting times for a group of participants. The application is leveraging Graph API to fetch timings for individual participants(screenshot below).

Issue: When fetching meeting times for multiple participants, the intersection time-slots are received as duplicates. Example: The data-flow to fetch intersection time-slot availability returns ‘n’ duplicate time-slot(in an array) for ‘n’ participants. Screenshot below displaying duplicate time-slots.

Question: Can we transform the resultant array(whose elements are unknown time-slots) to display the count of each elements in the array(instead of duplicates)?

Thanks!

1 Like

You can use query syntax to create another object to display the count of each element.

If foo is your array you can do something like this:

FROM i IN foo SELECT DISTINCT { "value": i, "count": LENGTH(FROM j IN foo WHERE j = i) }

This will output something like this:

[
  {
    "value": {
      "start": {
        "dateTime": "2022-01-06T10:00",
        "timeZone": "Eastern Standard Time"
      },
      "end": {
        "dateTime": "2022-01-06T10:00",
        "timeZone": "Eastern Standard Time"
      }
    },
    "count": 2
  }
]
2 Likes