Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Christopher Clemente
- //Comp-171
- //Hw #10
- import java.util.ArrayList;
- import java.util.Scanner;
- public class hw {
- public static void main(String[] args) {
- Scanner keyboard = new Scanner(System.in);
- System.out.println("Enter the size of the Array.");
- int size = keyboard.nextInt();
- int [] array = new int[size];
- System.out.println("Enter numbers into the array.");
- int count = 0;
- for (int i = 0; i < size; i++){
- int x = keyboard.nextInt();
- array[count] = x;
- count++;
- }
- System.out.println("The array with all the duplicates removed is " + newArray(array));
- }
- public static ArrayList<Integer> newArray(int[] array){
- int size = array.length;
- int numCount = 0;
- int count=0;
- ArrayList<Integer> array2 = new ArrayList<Integer>();
- for(int i = 0; i < size; i++) {
- count = 0;
- for(int r = 0; r<size; r++) {
- if (array[r] == array[i]) {
- count++;
- }
- }
- if(count == 1) {
- array2.add(array[i]);
- }
- }
- return array2;
- }
- }
- /*
- Enter the size of the Array.
- 10
- Enter numbers into the array.
- 1
- 7
- 3
- 5
- 7
- 1
- 0
- 4
- 6
- 2
- The array with all the duplicates removed is [3, 5, 0, 4, 6, 2]
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement