• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Java Arrays

AMD_RULES

2[H]4U
Joined
Mar 26, 2007
Messages
3,010
I need help with my code. I need it to return true showing the list contains duplicates. As it sits now, it always returns false.

Code:
public static boolean hasDuplicates(int[] values)
    {
          int[] numbers = values;
          boolean found = false;
          int value = 0;
          for (int i = 0; i < numbers.length; i++){
                  if(value == numbers[i]){
                      found = true;
                  }
                  value = numbers[i];
          }
          return found;
    }
 
Currently you have it set to that the function will only return true if the first number is 0 or number[i-1]==number. Both of these are not what you want.

Why are you creating a boolean variable that causes the loop to keep going when there's a duplicate instead of exiting (by using 'return')?
 
changed my code to this, but it still returns false.

Code:
boolean found = false;
          int value = 0;
          for (int i = 0; i < values.length; i++){
                  if(value == values[i]){
                      found = true;
                  }
                  value = values[i];
          }
          return found;
 
I have no idea what you are trying to do.
if it is a sorted array something like :

Code:
          for (int i = 0; i < values.length - 1; i++){
                  if(values[i] == values[i + 1]){
                      return true;
                  }
          }
          return false;

if it is not a sorted array then you will need to nest two for loops.

Code:
          for (int i = 0; i < values.length - 1; i++){
                    for (int j = i + 1; j < values.length; j++){
                            if(values[i] == values[j]){
                                return true;
                            }
                    }
          }
          return false;
 
Currently you have it set to that the function will only return true if the first number is 0 or number[i-1]==number. Both of these are not what you want.

Why are you creating a boolean variable that causes the loop to keep going when there's a duplicate instead of exiting (by using 'return')?

I'm trying to write a method that will determine if there are any duplicate integers in the array.
I have no idea what you are trying to do.
if it is a sorted array something like :

Code:
          for (int i = 0; i < values.length - 1; i++){
                  if(values[i] == values[i + 1]){
                      return true;
                  }
          }
          return false;

if it is not a sorted array then you will need to nest two for loops.

Code:
          for (int i = 0; i < values.length - 1; i++){
                    for (int j = i + 1; j < values.length; j++){
                            if(values[i] == values[j]){
                                return true;
                            }
                    }
          }
          return false;
Those both return "FALSE." isn't it suppose to return true?
 
Ignore my last comment.. I figured how to input. But I still can't get it to work fully.

Here's what I got :

Code:
boolean found = false;
          int value = 0;
          for (int i = 0; i < values.length; i++){
                  if(value == values[i]){
                      found = true;
                  }
                  if(value < values[i]){
                      value = values[i];
   
          }
          return found;
 
Got it! :D

Code:
public static boolean hasDuplicates(int[] values)
    {
         boolean found = false;
         for (int i = 0; i < values.length; i++){
             for (int r = i+1; r < values.length; r++){
                 if (values[i] == values[r]){
                     found = true;
                 }
             }
         }
         return found;
    }
 
so, issues with the solution: once a true condition is found it does wasteful processing through the rest of the for loops. That is why a return would work better. Also the first loop is going up to values.length. We know at this point the second loop will fail, since r would be assigned values.length + 1 which is greater than the maximum value. So the correct implementation for the first loop will use values.length - 1. Stuff like this might seem trivial or unimportant. But in some scenarios you multiply by a million transactions and it all adds up to additional cpu time.
 
What would be a better way to code this then?

He told you. I think he is mostly correct about what he said.

As for the second loop, when you reach the last item in the array number (be that 5, 500, 5000), it will error out. It will be searching for spot 6, 501, or 5001 in the array, and since they dont exist you will get a nice error.

Did that compile, and give you an error when you hit the last array item?

Btw-my Java professor ingrains in our head to constantly test our code, I write a few lines, test, write a few test. I would recommend that.
 
Those both return "FALSE." isn't it suppose to return true?
Try reading the whole thing... Come on, they're only five lines each.

What would be a better way to code this then?
Like jim said, once you find a duplicate, you're done; at this point, you can (and should) return. If you're still not sure, take a look at the solution he gave you...

As for the second loop, when you reach the last item in the array number (be that 5, 500, 5000), it will error out.

Did that compile, and give you an error when you hit the last array item?
It's fine. When you get to the last item, the second loop won't execute at all.
 
Try reading the whole thing... Come on, they're only five lines each.


Like jim said, once you find a duplicate, you're done; at this point, you can (and should) return. If you're still not sure, take a look at the solution he gave you...


It's fine. When you get to the last item, the second loop won't execute at all.



I see what you are saying, he is reading a single array and searching one array. my bad!
 
Yeah, sorting is a more efficient way of going about it. Though the API page you linked to has no sort function; you want something like java.util.Arrays.

A hash table would probably be even better.
 
hash table vs sorted list vs unsorted list depends on the size of your expected input. It also depends if the data input is sensitive to modification.

Remember, the kind of things that will make a function work very well on huge data inputs might cause it to be wicked slow if you are only processing tiny inputs.
 
Back
Top