How to optimize two for loops (a for loop inside a for loop) in java
<p>I have following problem:</p>

<ul>
<li>I have a very long string <code>String str = "abcdefghiart----"</code></li>
<li>Now I want to iterate over string and want to find the first duplicate character in string</li>
<li>This I can achieve with applying two for loops to search first duplicate</li>
<li><p>This approach works fine but iteration complexity will be very high in case string very large and only last two characters are duplicate</p>

<p>Now I want to minimize the complexity and optimize this code. I can use <code>foreach</code> loop too to iterate but still it will be two <code>foreach</code> loop. I don't want to use any system library. Can someone help me out on this?</p></li>
</ul>
