Probability distribution of $X_N$, $N = min\{n \geq 2: X_n = $ second largest of $ X_1, \ldots, X_n\}$

Let $f(x)$ be the density function of the $X_i$, and $F(x)$ their cdf.

Let $Z$ be the maximum of the $X_i$. For completeness we find the distribution of $Z$, though that is likely familiar to you.

The event $Z\le z$ happens precisely if all the $X_i$ are $\le z$. This has probability $(F(z))^n$. Thus $$F_Z(z)=(F(z))^n.$$ For the density function of $Z$, differentiate. We get $f_Z(z)=nf(z)(F(z))^{n-1}$.


Let $Y$ be the second largest of the $X_i$. We give a highly informal derivation of the density function $f_Y(y)$ of $Y$.

Let $dy$ be "small." We find the probability that $Y$ lies between $y$ and $y+dy$. This will be approximately $f_Y(y)\,dy$.

Neglecting terms in higher powers of $dy$, the probability that the second largest lies between $y$ and $y+dy$ is the probability that some $X_i$ lies in this interval times the probability that $n-2$ of the $X_i$ lie below $y$ and $1$ lies above $y+dy$.

The $X_i$ that lies between $y$ and $y+dy$ can be chosen in $n$ ways. The probability it lies in the interval is approximately $F(y)\,dy$.

The probability that $n-2$ of the remaining $X_i$ lie below $y$, and $1$ lies above, is $\binom{n-1}{1}(1-F(y))^1 (F(y))^{n-2}$. "Thus," $$f_Y(y)=n\binom{n-1}{1}f(y)(1-F(y))(F(y))^{n-1}.$$ For the cdf $F_Y(y)$, integrate from $-\infty$ to $y$. The integral is in principle easy, make the substitution $u=F(y)$.


A different way to show André Nicolas' result:

Let $Y$ be the second largest $X_i$. Then $Y \le x$ if and only if there is at most one $i$ such that $X_i > x$. Let $F(x) = \mathbb{P}(X_1 \le x)$ be the distribution function of $X_1.$ Using the fact that the $X_i$ are i.i.d.: $$ \begin{align} \mathbb{P}(Y \le x) &= \mathbb{P}(\{\exists! i: X_i > x\}) + \mathbb{P}(\{\forall i: X_i \le x\})\\ &= \sum_i \mathbb{P}(X_1 \le x, \dots, X_{i-1} \le x, X_i > x, X_{i+1} \le x, \dots, X_n \le x) + \mathbb{P}(X_1 \le x, \dots, X_n \le x) \\ &= \sum_i \mathbb{P}(X_i > x) \prod_{j \neq i} \mathbb{P}(X_j \le x) + \prod_i \mathbb{P}(X_i \le x) \\ &= \sum_i (1-F(x)) F(x)^{n-1} + F(x)^n \\ &= n (1-F(x)) F(x)^{n-1} + F(x)^n. \end{align} $$

Differentiate to get the density $f_Y$.


P.S.: One can also show $\mathbb{P}(Z > x, Y \le x) = \mathbb{P}(Y \le x) - \mathbb{P}(Z \le x)$, even if the $X_i$ are not i.i.d.