Exit Exit - 1 year ago 44
Java Question

More than one in an arrayList with user input

in the code below I need to check if in the ArrayList are more than one of same number. the user imput a number to consult, and if it exist in the array and there are more than one the message "appears more than one is displayed, i have been trying but i cant solve it.


ArrayList<Integer> list = new ArrayList<>();

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.");


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


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