Advertisement
RazorBlade57

hw10

Nov 22nd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. //Christopher Clemente
  2. //Comp-171
  3. //Hw #10
  4.  
  5.  
  6. import java.util.ArrayList;
  7. import java.util.Scanner;
  8. public class hw {
  9.  
  10.     public static void main(String[] args) {
  11.        
  12.         Scanner keyboard = new Scanner(System.in);
  13.        
  14.         System.out.println("Enter the size of the Array.");
  15.        
  16.         int size = keyboard.nextInt();
  17.         int [] array = new int[size];  
  18.                
  19.         System.out.println("Enter numbers into the array.");
  20.        
  21.         int count = 0;
  22.        
  23.         for (int i = 0; i < size; i++){
  24.             int x = keyboard.nextInt();
  25.             array[count] = x;
  26.             count++;
  27.         }
  28.        
  29.         System.out.println("The array with all the duplicates removed is " + newArray(array));
  30.        
  31.     }
  32.    
  33.     public static ArrayList<Integer> newArray(int[] array){
  34.        
  35.         int size = array.length;
  36.         int numCount = 0;
  37.         int count=0;
  38.         ArrayList<Integer> array2 = new ArrayList<Integer>();
  39.        
  40.         for(int i = 0; i < size; i++) {
  41.             count = 0;
  42.             for(int r = 0; r<size; r++) {
  43.                 if (array[r] == array[i]) {
  44.                 count++;
  45.                 }
  46.             }
  47.            
  48.             if(count == 1) {
  49.                 array2.add(array[i]);
  50.             }
  51.            
  52.         }
  53.         return array2;
  54.          
  55.     }
  56. }
  57.  
  58.  
  59.        
  60. /*
  61. Enter the size of the Array.
  62. 10
  63. Enter numbers into the array.
  64. 1
  65. 7
  66. 3
  67. 5
  68. 7
  69. 1
  70. 0
  71. 4
  72. 6
  73. 2
  74. The array with all the duplicates removed is [3, 5, 0, 4, 6, 2]
  75.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement