TheBulgarianWolf

Check for duplicate elements in an array

Jan 24th, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.io.*;
  5. import java.util.*;
  6.  
  7. class Enters {
  8.   public static void main(String[] args) {
  9.    
  10.     Scanner scanner = new Scanner(System.in);
  11.    
  12.     ArrayList<Integer> arr = new ArrayList<Integer>();
  13.     try{
  14.      
  15.        System.out.println("Enter numbers:");
  16.         int i;
  17.         int j;
  18.         while (true) {
  19.             int count = 0;
  20.             int input = scanner.nextInt();
  21.             arr.add(input);
  22.          for (int temp : arr) {
  23.             if(temp == input){
  24.               count++;
  25.             }
  26.         }
  27.         if(count==2){
  28.           System.out.print("The number is entered twice.");
  29.          
  30.         }
  31.         else if(count>2){
  32.           System.out.println("The number is entered more than 2 times.");
  33.          
  34.         }  
  35.         }
  36.     }catch(Exception e) {
  37.     //  Block of code to handle errors(In this case it stops the program)
  38.      
  39.       System.out.println("End of the program!");
  40.      
  41.     }
  42.    
  43.   }
  44. }
Add Comment
Please, Sign In to add comment