Search code examples
javaarraysif-statementcontinue

Counting the number of common elements in integer arrays located at different positions


For my assignment, I need to write a method that returns the number of cows (see definition below) found between 2 arrays. If the input arrays have a different number of elements, then the method should throw an IllegalArgumentException with an appropriate message.

A bull is a common number in int arrays found at the same position while a cow is a common number in int arrays found at different position. Note that if a number is already a bull, it cannot be considered as a cow.

For example, considering the following arrays:

int[] secret = {2, 0, 6, 9};
int[] guessOne = {9, 5, 6, 2};
int[] guessTwo = {2, 0, 6, 2};
int[] guessThree = {1, 2, 3, 4, 5, 6};
int[] guessFour = {1, 3, 4, 4, 0, 5};

1) getNumOfCows(secret, guessOne) returns 2
2) getNumOfCows(secret, guessTwo) returns 0
3) getNumOfCows(secret, guessThree) returns an exception
4) getNumOfCows(guessThree, guessFour) returns 2

My method seen below works perfectly for examples 1 and 3, but there is a problem with examples 2 and 4 such that getNumOfCows(secret, guessTwo) returns 1 instead of 0 because the element at secret[0] and guessTwo[3] is considered a cow. Could anybody help me fix my code?

// A method that gets the number of cows in a guess --- TO BE FIXED

  public static int getNumOfCows(int[] secretNumber, int[] guessedNumber) {

    // Initialize and declare a variable that acts as a counter

    int numberOfCows = 0;

    // Initialize and declare an array

    int[] verified = new int[secretNumber.length];

    if (guessedNumber.length == secretNumber.length) {

      // Loop through all the elements of both arrays to see if there is any matching digit

      for (int i = 0; i < guessedNumber.length; i++) {

        // Check if the digits represent a bull

        if (guessedNumber[i] == secretNumber[i]) {

          verified[i] = 1;
        }
      }

      for (int i = 0; i < guessedNumber.length; i++) {

        // Continue to the next iteration if the digits represent a bull

        if (verified[i] == 1) {

          continue;
        }

        else {

          for (int j = 0; j < secretNumber.length; j++) {

            if (guessedNumber[i] == secretNumber[j] && i != j) {

              // Update the variable

              numberOfCows++;

              verified[i] = 1;

            }
          }
        }
      }
    }

    else {

      // Throw an IllegalArgumentException

      throw new IllegalArgumentException ("Both array must contain the same number of elements");
    }

    return numberOfCows;
  }

Solution

  • First go through and mark all bulls using a separate array to make sure a position that is a bull also get counted as a cow

    public static int getNumOfCows(int[] secretNumber, int[] guessedNumber) {
        int max = secretNumber.length;
        int cows = 0;
        int[] checked = new int[max];
        for (int i = 0; i < max; i++) {
            if (secretNumber[i] == guessedNumber[i]) {
              checked[i] = 1;
            }
        }
    
        for (int i = 0; i < max; i++) {
          if (checked[i] == 1) {
            continue;
          }
          for (int j = 0; j < max; j++) {
            if (secretNumber[i] == guessedNumber[j]) {
              cows++;
              checked[i] = 1;
            }
          }
        }
        return cows;
    }
    

    Now that this answer is accepted the original question can be voted to be closed as a duplicate

    I am posting my answer from a duplicate question here and if this get approved then the other one can get closed as a duplicate.