# Counting Objects in Array for Crowdsourced Categorization

**URL:** <https://community.glideapps.com/t/counting-objects-in-array-for-crowdsourced-categorization/41032>\
**Category:** Ask for Help\
**Created:** [April 21, 2022, 8:59pm UTC](https://community.glideapps.com/t/counting-objects-in-array-for-crowdsourced-categorization/41032 "2022-04-21T20:59:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![todd.y](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/todd.y/32/41350_2.png) [@todd.y](https://community.glideapps.com/u/todd.y)\
**Post date:** [April 21, 2022, 8:59pm UTC](https://community.glideapps.com/t/counting-objects-in-array-for-crowdsourced-categorization/41032/1 "2022-04-21T20:59:14Z")

</div>

Hey Glide Community,

I’m working on an application that enables “giving circles” for a community, simulating a hybrid vehicle that borrows from the dynamics of both an endowment fund and a directed trust. The goal is to create a perpetual “community-directed giving pool” that directs gifts to beneficiaries identified by contributors.

One of the features of this tool will enable contributors to identify and cluster potential grant/gift recipients into categories of benefit (environmental, scientific, charitable, etc.)

**On to the question…**

I have a form that allows contributors to categorize a beneficiary (somewhat similar to creating a review).

 ![Categorize](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/b/6/b6ca3337c03cb1a89018a7f2208793a0e79ffe82.png)

Having created a relational column for this categorization system, I’m able to view the “related categories” of a given beneficiary.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/7/1/710a41bbaa87bdac4d6d1f3c80d710b04ce5682f.png)

**On to the question…**

Right now, the green banner (at the top) with **focus category** is set manually. Is there a way to identify which entries appear the most in an array, and set a column contents to a value in the entry? Using these submissions, I want to be able to identify the highest aligned category and have a column that changes when one category may overtake another.

This is what the back-end looks like. You can see that the beneficiary on the bottom row has two categorizations as “environmental” so I would want the “category” column to match the output of that function.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/4/0/401db4da166c4d18c378c99130c0bf0b4a543d31.png)

As always, any help is appreciated!

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [April 22, 2022, 12:52am UTC](https://community.glideapps.com/t/counting-objects-in-array-for-crowdsourced-categorization/41032/2 "2022-04-22T00:52:49Z")

</div>

![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/b/c/bcd0fcd440ca63ea610780cce575a1787fc966a1.png)

Here’s my draft.

First, you convert your array into a comma-delimited list by using a joined list column, with the delimiter being a single comma (no extra spaces).

Then, you add a JavaScript column and use this code.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/c/1/c1ac856a7769ac6dfae7740d0bab8adae0201287.png)

```auto
function mode(array)
{
    if(array.length == 0)
        return null;
    var modeMap = {};
    var maxEl = array[0], maxCount = 1;
    for(var i = 0; i < array.length; i++)
    {
        var el = array[i];
        if(modeMap[el] == null)
            modeMap[el] = 1;
        else
            modeMap[el]++;  
        if(modeMap[el] > maxCount)
        {
            maxEl = el;
            maxCount = modeMap[el];
        }
    }
    return maxEl;
}

let arr = p1.split(",");
return mode(arr)

```

Adapted from:

> <https://stackoverflow.com/questions/1053843/get-the-element-with-the-highest-occurrence-in-an-array/1053865#1053865>

---

<div class="post-metadata">

**Author:** ![todd.y](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/todd.y/32/41350_2.png) [@todd.y](https://community.glideapps.com/u/todd.y)\
**Post date:** [April 22, 2022, 1:51am UTC](https://community.glideapps.com/t/counting-objects-in-array-for-crowdsourced-categorization/41032/3 "2022-04-22T01:51:13Z")

</div>

> [@ThinhDinh](#):
>
> ```auto
> function mode(array)
> {
> if(array.length == 0)
> return null;
> var modeMap = {};
> var maxEl = array[0], maxCount = 1;
> for(var i = 0; i < array.length; i++)
> {
> var el = array[i];
> if(modeMap[el] == null)
> modeMap[el] = 1;
> else
> modeMap[el]++;  
> if(modeMap[el] > maxCount)
> {
> maxEl = el;
> maxCount = modeMap[el];
> }
> }
> return maxEl;
> }
> 
> let arr = p1.split(",");
> return mode(arr)
> 
> ```

Wow, that worked like charm, @ThinhDinh!

You have my sincerest thanks.

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [April 22, 2022, 2:00am UTC](https://community.glideapps.com/t/counting-objects-in-array-for-crowdsourced-categorization/41032/4 "2022-04-22T02:00:00Z")

</div>

It doesn’t handle ties though (say there are 2 or more categories with the same max occurrence). If you want that to be handled we can change the code a bit.

---

<div class="post-metadata">

**Author:** ![todd.y](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/todd.y/32/41350_2.png) [@todd.y](https://community.glideapps.com/u/todd.y)\
**Post date:** [April 22, 2022, 4:59pm UTC](https://community.glideapps.com/t/counting-objects-in-array-for-crowdsourced-categorization/41032/5 "2022-04-22T16:59:02Z")

</div>

That’s also helpful to know. I’m expecting this feature to be used on a daily basis, so I don’t expect ties to occur too often, but it would be something to address in the future. Would the code error, show two values, or just keep one - likely the preceding value?

Thinh, you saved me many hours of time! Thank you! 🙌

Generally, I would be interested in getting your feedback and advisory eye on the app and our architecture. Do you mind if I reach out over PM to discuss potential support?

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [April 23, 2022, 1:26am UTC](https://community.glideapps.com/t/counting-objects-in-array-for-crowdsourced-categorization/41032/6 "2022-04-23T01:26:22Z")

</div>

> [@todd.y](#):
>
> or just keep one - likely the preceding value?

It will always show the first one among the ties.

> [@todd.y](#):
>
> Do you mind if I reach out over PM to discuss potential support?

I may not have enough time to support you, so if you have any questions please post it publicly in this forum, many people will be around to help.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/system/32/53398_2.png) [@system](https://community.glideapps.com/u/system)\
**Post date:** [April 24, 2022, 1:26am UTC](https://community.glideapps.com/t/counting-objects-in-array-for-crowdsourced-categorization/41032/7 "2022-04-24T01:26:42Z")

</div>

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.
