# Return the most repeated value or values in an array

**URL:** <https://community.glideapps.com/t/return-the-most-repeated-value-or-values-in-an-array/59103>\
**Category:** Ask for Help\
**Created:** [March 13, 2023, 7:29pm UTC](https://community.glideapps.com/t/return-the-most-repeated-value-or-values-in-an-array/59103 "2023-03-13T19:29:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ovi](https://avatars.discourse-cdn.com/v4/letter/o/3ab097/32.png) [@Ovi](https://community.glideapps.com/u/Ovi)\
**Post date:** [March 13, 2023, 7:29pm UTC](https://community.glideapps.com/t/return-the-most-repeated-value-or-values-in-an-array/59103/1 "2023-03-13T19:29:02Z")

</div>

Is there a way to have a result like the one below in a column ?

array : 1,2,3,4,2,3,4,2,2,3,2,2  
result : 2

I see that the MODE function is available for excel but is not available in excel functions for Glide … it will be great to have it, also I see that JS is also limited to similar functions like excel functions

or is there a way to have multiple filters from different tables … like if option A is selected, filter option B, and if Option B is selected, filter option C, but because every row can have multiple values , the value in those columns are arrays , so how I think that this could be solved , is only by seeing the id’s that are repeating the most in the final array that joines all the values … any other suggested is appreciated

Thanks

---

<div class="post-metadata">

**Author:** ![Robert\_Petitto](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/robert_petitto/32/25193_2.png) [@Robert\_Petitto](https://community.glideapps.com/u/Robert_Petitto)\
**Post date:** [March 13, 2023, 10:36pm UTC](https://community.glideapps.com/t/return-the-most-repeated-value-or-values-in-an-array/59103/2 "2023-03-13T22:36:32Z")

</div>

Sounds like a good case for the javascript column:

```auto
function findMode(csvString) {
  // Split the string into an array of numbers
  let numbers = csvString.split(",").map(Number);
  
  // Count the frequency of each number using an object
  let frequency = {};
  for (let i = 0; i < numbers.length; i++) {
    if (frequency[numbers[i]] === undefined) {
      frequency[numbers[i]] = 1;
    } else {
      frequency[numbers[i]]++;
    }
  }
  
  // Find the maximum frequency
  let maxFrequency = 0;
  for (let number in frequency) {
    if (frequency.hasOwnProperty(number)) {
      if (frequency[number] > maxFrequency) {
        maxFrequency = frequency[number];
      }
    }
  }
  
  // Collect the numbers with the maximum frequency
  let modes = [];
  for (let number in frequency) {
    if (frequency.hasOwnProperty(number)) {
      if (frequency[number] === maxFrequency) {
        modes.push(number);
      }
    }
  }
  
  // Join the modes array into a comma-separated string and return it
  return modes.join(",");
}

return mode = findMode(p1);

```

Where `p1` is a CSV of values like in your example:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/4/3/43c89059f491e6b022288ddcc366baf44ef73674.jpeg)

Ties result in CSV of modes:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/b/0/b03a19e635fc15f1d9504678810e0a46d2e97a3b.png)

---

<div class="post-metadata">

**Author:** ![Ovi](https://avatars.discourse-cdn.com/v4/letter/o/3ab097/32.png) [@Ovi](https://community.glideapps.com/u/Ovi)\
**Post date:** [March 13, 2023, 10:58pm UTC](https://community.glideapps.com/t/return-the-most-repeated-value-or-values-in-an-array/59103/3 "2023-03-13T22:58:30Z")

</div>

@Robert_Petitto Thank you very much

---

<div class="post-metadata">

**Author:** ![Ovi](https://avatars.discourse-cdn.com/v4/letter/o/3ab097/32.png) [@Ovi](https://community.glideapps.com/u/Ovi)\
**Post date:** [March 14, 2023, 12:46pm UTC](https://community.glideapps.com/t/return-the-most-repeated-value-or-values-in-an-array/59103/4 "2023-03-14T12:46:43Z")

</div>

I’ve found a solution changing the text number to string inside the formula and it seems to be working … not sure if what I did is good because I’m not a programmer but it worked

here is the code

```auto
function findMode(csvString) {
  // Split the string into an array of numbers
  let numbers = csvString.split(",").map(String);
  
  // Count the frequency of each number using an object
  let frequency = {};
  for (let i = 0; i < numbers.length; i++) {
    if (frequency[numbers[i]] === undefined) {
      frequency[numbers[i]] = 1;
    } else {
      frequency[numbers[i]]++;
    }
  }
  
  // Find the maximum frequency
  let maxFrequency = 0;
  for (let string in frequency) {
    if (frequency.hasOwnProperty(string)) {
      if (frequency[string] > maxFrequency) {
        maxFrequency = frequency[string];
      }
    }
  }
  
  // Collect the numbers with the maximum frequency
  let modes = [];
  for (let string in frequency) {
    if (frequency.hasOwnProperty(string)) {
      if (frequency[string] === maxFrequency) {
        modes.push(string);
      }
    }
  }
  
  // Join the modes array into a comma-separated string and return it
  return modes.join(",");
}

return mode = findMode(p1);

```

---

<div class="post-metadata">

**Author:** ![Dilon\_Perera](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/dilon_perera/32/85652_2.png) [@Dilon\_Perera](https://community.glideapps.com/u/Dilon_Perera)\
**Post date:** [March 14, 2023, 3:27pm UTC](https://community.glideapps.com/t/return-the-most-repeated-value-or-values-in-an-array/59103/6 "2023-03-14T15:27:04Z")

</div>

@Ovi correct?

![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/3/2/329d82574506388a31915d43922d75b0ff03f960.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/2/4/242d9397a58fccab8158611a4e65d0e40d8bc1e5.png)

 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/6/0/60e95500cc19f13f95d44219e582a9dfbd79b74c.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/9/2/92cff1a61e23b26f6b028e6fabf5e8ffe5d2f528.png)

> **JavaScript Code**
>
> ```auto
> function findMostCommonValues(list) {
> let countMap = list.split(',')
> .reduce((acc, curr) => ((acc[curr] = (acc[curr] || 0) + 1), acc), {});
> 
> let highestCount = Math.max(...Object.values(countMap));
> return Object.keys(countMap).filter(key => countMap[key] === highestCount);
> }
> 
> let list = p1;
> let mostCommonValues = findMostCommonValues(list);
> return mostCommonValues.toString();
> 
> ```

Thank you

---

<div class="post-metadata">

**Author:** ![Ovi](https://avatars.discourse-cdn.com/v4/letter/o/3ab097/32.png) [@Ovi](https://community.glideapps.com/u/Ovi)\
**Post date:** [March 14, 2023, 4:13pm UTC](https://community.glideapps.com/t/return-the-most-repeated-value-or-values-in-an-array/59103/7 "2023-03-14T16:13:00Z")

</div>

@Dilon_Perera Perfect … Thanks

---

<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:** [March 15, 2023, 4:13pm UTC](https://community.glideapps.com/t/return-the-most-repeated-value-or-values-in-an-array/59103/8 "2023-03-15T16:13:36Z")

</div>

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