I'm attempting to prove that, if $U=(U^i_j)_{n \times n}$ is the matrix associated to an orthogonal transformation and $g=(g_{ij})_{n \times n}$ is the metric tensor, then
$$U^{T} g U=g \tag{1}$$
My attempt
Applying the condition that an orthogonal transformation must preserve the scalar product, $\big(U(\boldsymbol{x}), U(\boldsymbol{y})\big) = (\boldsymbol{x}, \boldsymbol{y})$, I have found that that
$$ \sum ^n _{i=1} \sum ^n _{j=1} \sum ^n _{k=1} \sum ^n _{l=1} g_{i j} U_{k}^{i} U_{l}^{j} x^{k} y^{l}= \sum ^n _{i=1}\sum ^n _{j=1} g_{i j} x^{i} y^{j} \tag{2}$$
Using the Einstein summation convention and changing in the RHS the indexes $i \rightarrow k, j \rightarrow l$
$$g_{i j} U_{k}^{i} U_{l}^{j} x^{k} y^{l} = g_{kl} x^{k} y^{l} \tag{3}$$
Since the terms $x^{k} y^{l}$ appears at both sides, if we could identify $g_{i j} U_{k}^{i} U_{l}^{j}=g_{k l}$, this would correspond to the matrix product $U^{T} g U=g$, and the proof would be complete.
But I'm stuck here, because I don't see how we can match parts of different sums... Could someone tell me how could it be justified from $(3)$ that $g_{i j} U_{k}^{i} U_{l}^{j}=g_{k l}$ (if it is actually possible to do this)?
As already said, the key is that the equality must be satisfied for any values of $\boldsymbol{x}=(x^1,...,x^n)$ and $\boldsymbol{y}=(y^1,...,y^n)$. Also notice that in the LHS, once the sum is performed over $i$ and $j$, there is a term that only depends on $k$ and $l$. This is clear by reordering equation $(3)$ as follows:
$$ \sum ^n _{k=1} \sum ^n _{l=1} x^{k} y^{l} \bigg(\sum ^n _{i=1} \sum ^n _{j=1} g_{i j} U_{k}^{i} U_{l}^{j} \bigg) = \sum ^n _{k=1}\sum ^n _{l=1} x^{k} y^{l}g_{kl} $$
$$ \sum ^n _{k=1} \sum ^n _{l=1} x^{k} y^{l} h_{kl} = \sum ^n _{k=1}\sum ^n _{l=1} x^{k} y^{l}g_{kl} $$
So $h_{kl}=g_{i j} U_{k}^{i} U_{l}^{j}=g_{kl}$.