mode
Identifies the most frequently occurring number in a given list of numbers, enabling quick and accurate statistical analysis for data-driven tasks.
Instructions
Finds the most common number in a list of numbers
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| numbers | Yes | Array of numbers to find the mode of |
Implementation Reference
- build/index.js:150-158 (handler)Handler function that executes the 'mode' tool: invokes Statistics.mode on input numbers and returns MCP-formatted text response with mode values and frequency.}, async ({ numbers }) => { const value = Statistics.mode(numbers); return { content: [{ type: "text", text: `Entries (${value.modeResult.join(', ')}) appeared ${value.maxFrequency} times` }] }; });
- build/index.js:148-158 (registration)Registration of the 'mode' MCP tool on the mathServer, including name, description, input schema, and handler function.mathServer.tool("mode", "Finds the most common number in a list of numbers", { numbers: z.array(z.number()).describe("Array of numbers to find the mode of") }, async ({ numbers }) => { const value = Statistics.mode(numbers); return { content: [{ type: "text", text: `Entries (${value.modeResult.join(', ')}) appeared ${value.maxFrequency} times` }] }; });
- build/index.js:149-149 (schema)Zod input schema for the 'mode' tool: requires an array of numbers.numbers: z.array(z.number()).describe("Array of numbers to find the mode of")
- build/Classes/Statistics.js:39-68 (helper)Helper function Statistics.mode that computes the mode(s) of an array: uses Map for frequency count, identifies max frequency, collects all values with that frequency.static mode(numbers) { const modeMap = new Map(); //Set each entry parameter into the map and assign it the number of times it appears in the list numbers.forEach((value) => { if (modeMap.has(value)) { modeMap.set(value, modeMap.get(value) + 1); } else { modeMap.set(value, 1); } }); //Find the max frequency in the map let maxFrequency = 0; for (const numberFrequency of modeMap.values()) { if (numberFrequency > maxFrequency) { maxFrequency = numberFrequency; } } const modeResult = []; //Find the entries with the highest frequency for (const [key, value] of modeMap.entries()) { if (value === maxFrequency) { modeResult.push(key); } } return { modeResult: modeResult, maxFrequency: maxFrequency }; }