title: Firstduplicate in array using java
body: <p>I am trying to solve a problem where I need to find the duplicate number with lower indices. </p>
  
  <p>For example if the array is <strong>{1,4,3,5,3,2,1}</strong> 
  then the answer should be <strong>3</strong>,  because it is in smaller indices then the next repeated integer 1. </p>
  
  <p>If no repeated number is found then needs to return -1. 
  I Need to write an program which impalement it in o(n) time complexity.</p>
  
  <p>I am able to solve it for the O(n^2) probability but dont know how to do it for O(n). Also i am not understanding how to implement if no repeated digits are found.
  Kindly help me. I am stuck here.</p>
  
  <p>Thank you in advance.</p>
  
  <p>My code so far:    </p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Collections;
  
  public class firstDuplicate
  {
      //defining array
      public static void main(String args[]) 
      {
       int[] a1 = new int[] {2,3,3,1,5,2};
  
      //int[] a1 = new int[] {2,3,4,1,5,8};
  
      for(int i=0;i&lt;a1.length; i++) 
      {
          System.out.println(a1[i]);
      }
      int counter =0;
      ArrayList&lt;Integer&gt; list1 = new ArrayList&lt;&gt;();
  
  
      for(int i =0; i&lt;a1.length; i++) 
      {
          for(int j=i+1;j&lt;a1.length; j++) 
          {
              if(a1[i]==a1[j])
              {
                  counter =counter+1;
                  System.out.println(j);
                  list1.add(j);
  
              }
              else {
                  int k =-1;
                  System.out.println(k);
              }
          }
  
  
      }
  
      System.out.println("Printing ur numbers of repetation");
      System.out.printf("%d",counter);
      System.out.println("If printed ur answer is following");
      int k = Collections.min(list1);
      System.out.println(k);
      }
  }
  </code></pre>