Return the Most Frequent Repeatable Value

This example uses the REPEATABLEVALUES() expression to return the most frequent value captured in a field in a repeatable section.

function mostFrequentValue(array) {
  if (array === null) {
    return null;
  }
  var frequencies = {};
  var mostFrequentCount = 0;
  var mostFrequentValue = null;
  array.forEach(function(value) {
    if (frequencies[value] === null) {
      frequencies[value] = 1;
    } else {
      frequencies[value] += 1;
    }
    if (frequencies[value] > mostFrequentCount) {
      mostFrequentCount = frequencies[value];
      mostFrequentValue = value;
    }
  });
  return mostFrequentValue;
}
var values = REPEATABLEVALUES($repeatable_section, 'field_in_repeatable');
var value = mostFrequentValue(values);
SETRESULT(value);