Example 4 Let $x_{1}, x_{2}, \cdots, x_{n}$ take values in some interval of length 1. Let $x=\frac{1}{n} \sum_{j=1}^{n} x_{j}, y=\frac{1}{n} \sum_{j=1}^{n} x_{j}^{2}$. Find the maximum value of the function $f=y-x^{2}$:
Let $x_{1}, \cdots, x_{n} \in [a, a+1] (a \in \mathbf{R})$.
When $n=1$, $f=0$, obviously, $f_{\text{max}}=0$.
When $n>1$, if we fix $x_{2}, x_{3}, \cdots, x_{n}$, then we have
$$
\begin{array}{l}
f=y-x^{2}=\frac{1}{n} \sum_{j=1}^{n} x_{j}^{2}-\left(\frac{1}{n} \sum_{j=1}^{n} x_{j}\right)^{2} \\
=\left(\frac{1}{n}-\frac{1}{n^{2}}\right) x_{1}^{2}-\frac{2 x_{1}}{n^{2}} \sum_{j=2}^{n} x_{j}+ \\
\frac{1}{n} \sum_{j=2}^{n} x_{j}^{2}-\left(\frac{1}{n} \sum_{j=2}^{n} x_{j}\right)^{2} .
\end{array}
$$

Since $f$ is a quadratic function of $x_{1}$ on $[a, a+1]$,
the coefficient of the quadratic term $\frac{1}{n}-\frac{1}{n^{2}}>0$, so,
$$
\begin{array}{l}
f \equiv f\left(x_{1}, x_{2}, \cdots, x_{n}\right) \\
\leqslant \max \left\{f\left(a, x_{2}, \cdots, x_{n}\right), f\left(a+1, x_{2}, \cdots, x_{n}\right)\right\} .
\end{array}
$$

By symmetry, we get
$$
\begin{array}{l}
f\left(x_{1}, x_{2}, \cdots, x_{n}\right) \\
\leqslant \max _{x_{i} \in\{a, a+1\}}\left\{f\left(x_{1}, x_{2}, \cdots, x_{n}\right)\right\}(1 \leqslant i \leqslant n) .
\end{array}
$$

Let $b=a+1$, suppose $x_{1}, x_{2}, \cdots, x_{n}$ have $s$ values of $x_{i}=a$, and $n-s$ values of $x_{i}=b$. Then
$$
\begin{array}{l} 
\max _{x_{i} \in\{a, b\}}\left\{f\left(x_{1}, x_{2}, \cdots, x_{n}\right)\right\} \\
= \frac{1}{n}\left[s a^{2}+(n-s) b^{2}\right]- \\
{\left[\frac{1}{n}(s a+(n-s) b)\right]^{2} } \\
= \frac{1}{n^{2}} s(n-s)(b-a)^{2}=\frac{1}{n^{2}} s(n-s) \\
\leqslant \begin{array}{ll}
\frac{1}{4}, & n \text{ is even; } \\
\frac{n^{2}-1}{4 n^{2}}, n \text{ is odd. }
\end{array}
\end{array}
$$

The equality holds if and only if $x_{1}, x_{2}, \cdots, x_{n}$ have $\left[\frac{n+1}{2}\right]$ values of $a$ (or $a+1$), and the rest are $a+1$ (or $a$). Therefore,
$$
f_{\max }=\left\{\begin{array}{ll}\frac{1}{4}, & n \text{ is even; } \\ \frac{n^{2}-1}{4 n^{2}}, & n \text{ is odd. }\end{array}\right.
$$
Note: This method is typically applicable when both the conditions and the conclusion are symmetric expressions in $x_{1}, x_{2}, \cdots, x_{n}$.