-1

I'm trying to figure out how to check if all 23 numbers in my array (the numbers are randomly generated) are NOT equal to each other but I can't figure out how to do it without a super ridiculous if statement. Is there any other way I could do it? Another option would be to check if any two numbers in the array are equal but the reason I posed the question the way I did was because I figured checking the equality of each pair would be harder than checking the inequality of all the numbers.

3
  • Hi Patrick, what have you tried? Mess your hands, write some code and show it to us!
    – moonwave99
    Sep 6, 2015 at 21:23
  • Consider looking into for-loops. Sep 6, 2015 at 21:28
  • @moonwave99 Hey sorry for the late reply, I kind of forgot I had linked my facebook to stack overflow and ended up using a different account for a while and just ended up logging into this one again for the first time in a while. On the bright side, 3 years have passed, and I'm currently in my second year in college as a software engineering major! Yay! Mar 1, 2018 at 1:13

3 Answers 3

0

Your problem can be solved with two nested for-loops

public static boolean hasDuplicates(int[] array)
{
    for (int i = 0, length = array.length; i < length; i++)
    {
        int val = array[i];
        for (int j = 0; j < i; j++)
        {
            if (array[j] == val)
            {
                return true;
            }
        }
    }

    return false;
}

The first loop loops over every element in the array, while the second one checks if any of the values before the index of the outer loop are equal to the value at array[i]. You can safely use j < i for performance and to ensure that it doesn't return true on the same element.

0

You can use an algorithm like the following to test whether every array element is unique:

boolean everyNumberIsUnique(int[] numbers) {
  for(int i = 0; i < numbers.length; i++) {
    for(int j = i + 1; j < numbers.length; j++) {
      if(numbers[i] == numbers[j]) return false;
    }
  }
  return true;
}

It simply compares each number against each other and returns false if any two numbers are equal.

2
  • This won't work, it will compare array[1] with itself and return false right away.
    – Clashsoft
    Sep 6, 2015 at 21:23
  • @Clashsoft Thanks for the hint, classic mistake. Forgot to add +i.
    – Tobias
    Sep 6, 2015 at 21:27
0

If you have a relatively low upper bound on your numbers (say less than 1 million) and don't really care about memory usage, you can just create a boolean array and set each element to true if its index is in your array. This is O(n), so its probably the best you can do, but, admittedly, you have to meet the above criteria.

public boolean allDifferent(int[] numbers)
{
    //everything in the array defaults to false
    boolean[] array = new boolean[upperBound+1];

    for (int i = 0; i < numbers.length; i++)
    {
        if (array[numbers[i]])   //if we've already seen this number (aka duplicate)
        {
            return false;
        }

        array[numbers[i]] = true;   //note that we have now seen this number
    }

    return true;
}
2
  • A boolean array might not be the best solution since each element consumes an entire byte. A java.util.BitSet might be a faster alternative that doesn't have the drawbacks of a generic Set.
    – Clashsoft
    Sep 6, 2015 at 21:46
  • @Clashsoft I had no idea BitSet was a thing. But I did warn that although it has linear time complexity, this solution does use a bunch of memory.
    – Adam Evans
    Sep 6, 2015 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.