int[] arr = {4,6,2,2,6,6,1};int length = arr.length;int res = 0;for (int i = 0; i < length; i++)    for (int j = 0; j < length; j++)        if(arr[i] == arr[j]            res = Math.max(res, Math.abs(i-j));return res;For my work, I have written this chunk of code but does not seem to be optimized code (got as review comment). Can someone help me to optimize it. It is now \$\mathcal{O}(n^2)\$. I want \$\mathcal{O}(n)\$. The input is an array.
how can I optimize this chunk of code
java