Return the most repeated value or values in an array

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

Sounds like a good case for the javascript column:

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:

Ties result in CSV of modes:
image

2 Likes

@Robert_Petitto Thank you very much

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

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);

@Ovi correct?

image
image


image

JavaScript Code
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

2 Likes

@Dilon_Perera Perfect … Thanks

1 Like

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