3

In the code below I need to check if a number appears more than once in an ArrayList. The user inputs a number to consult, and if it exist in the array and there are more than one the messages, "appears more than once is displayed". I have been trying to do this but I don't know how.

This is the main class:

    ArrayList<Integer> list = new ArrayList<>();
    list.add(3);
    list.add(2);
    list.add(7);
    list.add(2);

    System.out.println("Type a number: ");
    int number = Integer.parseInt(sc.nextLine());
    if (Metodos.moreThanOne(list, number)) {
        System.out.println(number + " appears more than once.");
    } else {
        System.out.println(number + " does not appear more than once.");
    }

This is the method:

public static boolean moreThanOne(ArrayList<Integer> list, int number) {
    for (Integer in : list) {
        if (list.contains(number) && in==in) {
            return true;
        }
    }
    return false;
}
0

4 Answers 4

5

You can use this code:

if (list.lastIndexOf(element) != list.indexOf(element)) {
  return true; // you have at least two numbers
} else {
  return false; // element is not exist or you have only one element
}
Sign up to request clarification or add additional context in comments.

5 Comments

Why not directly this? return list.lastIndexOf(element) != list.indexOf(element);
@RolandIllig because if requested element is not exist you will get -1 from the left and from the right
But your code does exactly the same, just with more lines.
@RolandIllig excuse me, it is the same
Oh no, not this if true return true else false pattern again.
1
    public static boolean moreThanOne(ArrayList<Integer> list, int number) 
    {
      int count = 0;
      for (Integer in : list) {
          if (number == in) {
              count++;
          }
      }
      if(count > 1)
         return true;
      else 
         return false;
      }
    }

Comments

0

You can do this:

public class StartHere {
public static void main(String[] args) {

    ArrayList<Integer> list = new ArrayList<>();
    list.add(3);
    list.add(2);
    list.add(7);
    list.add(2);

    Scanner scanner = new Scanner(System.in);

    System.out.print("Type a number: ");
    String input = scanner.nextLine();
    int number = Integer.parseInt(input);
    scanner.close();
    if(moreThanOnce(list, number)){
        System.out.println("The number appears more than once.");
    }else{
        System.out.println("The number doesn't appear more than once.");
    }

}

public static boolean moreThanOnce(ArrayList<Integer> list, int number) {
    boolean bool = false;
    int counter = 0;
    for (Integer value : list) {
        if (value == number) {
            counter++;
        }
    }
    if (counter > 1){
        bool = true;
    }
    return bool;
}
}

Just a tip: you should improve your english, so it's easier to understand your question. A great way of doing this is reading books wrote in this language.

3 Comments

I have tried that, doesnt work, if you input 3 or 7, the message "The number appears more than once." is displayed, you are only checking if the number is on the list.
Ooooh... Wait a second, I'll fix it.
Is the same answer posted by Alikbar, Thanks Anyway
0
    System.out.println("Type a number: ");
    Set<Integer> set = new HashSet<Integer>(list);
    Scanner scanner = new Scanner(System.in);
    int number = Integer.parseInt(scanner.nextLine());

    if (!set.add(number)) {
        System.out.println(number + " appears more than once.");
    } else {
        System.out.println(number + " does not appear more than once.");
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.