user2843446
user2843446

Reputation: 59

Check duplicates of numbers input by user

I'm doing a program where user input five numbers and in the end the numbers are printed out which is working fine. What I can't get to work is a boolean function to check for duplicates. It should check for duplicates as the user write them in, so e.g. if number one is 5 and the second numbers is also 5, you should get an error until you write in a different number. Meaning if the user input a duplicate it should NOT be saved in the array. This is obviously an assignment, so I'm just asking for a hint or two.

This program is written based on pseudo-code given to me, and therefore I have to use a boolean to check for duplicates with the public boolean duplicate( int number ) class.

I've tried getting my head around it and tried something by myself, but obviously I'm doing a stupid mistake. E.g.:

if(int i != myNumbers[i]) 
   checkDuplicates = false
     else
       checkDuplicates = true;

return checkDuplicates;

DuplicatesTest class:

public class DuplicatesTest {

    public final static int AMOUNT = 5;

    public static void main(String[] args) {

        Duplicates d = new Duplicates(AMOUNT);
        d.inputNumber();
        d.duplicate(AMOUNT);
        d.printInputNumbers();
    }
}

Duplicates class:

public class Duplicates {

  private int amount;    
  private int[] myNumbers; 
  private boolean checkDuplicates;

  public Duplicates(int a) {

    amount = a;
    myNumbers = new int[amount];   
}

  public void inputNumber() {

    for(int i = 0; i < amount; i++ ) {
      int input = Integer.parseInt(JOptionPane.showInputDialog("Input 5 numbers"));
      myNumbers[i] = input;
    }   
}

  public boolean duplicate( int number ) {

    <BOOLEAN TO CHECK FOR DUPLICATES, RETURN FALSE OR TRUE>

  }

  public void printInputNumbers() {

    JTextArea output = new JTextArea();
    output.setText("Your numbers are:" + "\n");

    for(int i = 0; i < myNumbers.length; i++) {
      if (i % 5 == 0) {
        output.append("\n");
      }
      output.append(myNumbers[i] + "\t");
    }
    JOptionPane.showMessageDialog(null, output, "Numbers", JOptionPane.PLAIN_MESSAGE);   
  } 
}

Sorry if the code tag is messy, I had some trouble with white fields in between and such. I'm new here.

Upvotes: 3

Views: 4110

Answers (2)

Michael Yaworski
Michael Yaworski

Reputation: 13483

int nums[] = new int[5];
int count = 0;

public boolean duplicate(int number) 
{
    boolean isDup = false;

    for (int i = 0; i <= count; i++)
    {
        if (number == nums[i])
        {
            isDup = true;
            break;
        }
    }

    if (!isDup) 
    {
        count++;
        nums[count] = number;
    } 
    return isDup;
}    

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213223

Don't store the numbers in an array. Use a Set<Integer> instead. And then do a Set#contains() operation. It's O(1) operation which is actually far better than iterating over the array to search for duplicates.

Ok, if it's a compulsion to use an array, then you should modify your current approach, to return true as soon as you find a duplicate, instead of iterating over the array again. In your current approach, since you are setting the boolean variable to false in the else block, your method will return false if the last element of the array is not the same as what you are checking. So, just modify your approach to:

// loop over the array
    if (number == myNumbers[i])   
        return true;
// outside the loop, if you reach, return false
return false;

Note that your current if statement will not compile. You are declaring an int variable there, which you can't do.

if (int i == myNumbers[i])   // this is not a valid Java code.

Upvotes: 1

Related Questions