-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathExercise11_13.java
58 lines (40 loc) · 1.36 KB
/
Exercise11_13.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package ch_11;
import java.util.Scanner;
import java.util.ArrayList;
/**
* 11.13 (Remove duplicates) Write a method that removes the
* duplicate elements from an array list of integers using the following header:
* public static void removeDuplicate(ArrayList<Integer> list)
* <p>
* <p>
* Write a test program that prompts the user to enter 10 integers to a list
* and displays the distinct integers in their input order and
* separated by exactly one space.
*/
public class Exercise11_13 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Integer> testList = new ArrayList<>();
System.out.println("Enter ten integers to put into the list now:");
for (int i = 0; i < 10; i++) {
testList.add(input.nextInt());
}
removeDuplicate(testList);
System.out.print("The distinct integers are ");
for (Integer integer : testList) {
System.out.print(integer + " ");
}
input.close();
}
public static void removeDuplicate(ArrayList<Integer> list) {
Integer temp;
for (int i = 0; i < list.size(); i++) {
temp = list.get(i);
list.remove(i);
while (list.contains(temp)) {
list.remove(temp);
}
list.add(i, temp);
}
}
}