mode() : Find Most Common Value
On this page
Summary
Find the mode (most frequently occurring value) in a collection. Returns the value that appears most often.
Signature
mode(collection)
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
collection |
Array | Yes | The collection of values |
Returns
Type: Same as collection item type
The most frequently occurring value. If multiple values tie for most frequent, returns the first one encountered. Returns null for empty collections.
Examples
Basic Mode
Data:
doc.Params["model"] = new {
scores = new[] { 85, 90, 85, 92, 88, 85, 95 } // 85 appears 3 times
};
Output:
<p>Most common score: 85</p>
Statistical Summary
Data:
doc.Params["model"] = new {
scores = new[] { 78, 85, 85, 90, 85, 92, 88 }
};
Output:
<h3>Test Results</h3>
<p>Mean: 86.1</p>
<p>Median: 85</p>
<p>Mode: 85</p>
<p>The most common score was 85</p>
Most Popular Product
Data:
doc.Params["model"] = new {
orders = new[] {
new { orderId = 1, productId = "A001" },
new { orderId = 2, productId = "A002" },
new { orderId = 3, productId = "A001" },
new { orderId = 4, productId = "A003" },
new { orderId = 5, productId = "A001" }
}
};
Output:
<p>Most ordered product: A001</p>
Customer Preferences
Data:
doc.Params["model"] = new {
responses = new[] {
new { respondent = "User1", choice = "Option A" },
new { respondent = "User2", choice = "Option B" },
new { respondent = "User3", choice = "Option A" },
new { respondent = "User4", choice = "Option C" },
new { respondent = "User5", choice = "Option A" },
new { respondent = "User6", choice = "Option B" }
}
};
Output:
<h3>Survey Results</h3>
<p>Responses collected: 6</p>
<p>Most popular choice: Option A</p>
<p>3 people chose this option</p>
String Mode
Data:
doc.Params["model"] = new {
tasks = new[] {
new { id = 1, status = "active" },
new { id = 2, status = "completed" },
new { id = 3, status = "active" },
new { id = 4, status = "pending" },
new { id = 5, status = "active" }
}
};
Output:
<p>Most common status: active</p>
Notes
- Returns null for empty or null collections
- Works with any comparable type (numbers, strings, dates, etc.)
- If multiple values tie for most frequent, returns first encountered
- Case-sensitive for string comparisons
- Does not return frequency count (use
countOf()for that) - Useful for:
- Finding most popular items
- Survey analysis
- Identifying common patterns
- Customer preference analysis
- Quality control (most common defect)
- For property-based mode, use with
collect():mode(collect(items, 'property')) - Different from mean and median:
- Mean: arithmetic average
- Median: middle value
- Mode: most common value
- Can be used with non-numeric data (unlike mean/median)