2021-05-29

Return true if the array contains duplicate elements (which need not be adjacent)

When I execute this, even if there are no duplicates, it displays true. Basically, it displays true for every array that I put in. I have no idea why it's showing me that. I would appreciate some help in this problem! Thanks!

public static boolean Duplicate()
{
 int [] duplicateArray = new int[5];
 System.out.print("Enter your array numbers:");
 System.out.println();
 for(int i = 0; i < duplicateArray.length; i++)
 {
 Scanner in = new Scanner(System.in);
 duplicateArray[i] = in.nextInt();
 }
 boolean same = false;
 for(int i = 0; i < duplicateArray.length-1; i++)
 {
  for(int j = 0; j < duplicateArray.length; j++)
  {
  
   if(i==j)
   {
    same = true;
   }
  }
 }
 return same;
}


from Recent Questions - Stack Overflow https://ift.tt/3yOmBFx
https://ift.tt/eA8V8J

No comments:

Post a Comment