-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetExample1.java
103 lines (92 loc) · 1.97 KB
/
SetExample1.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package set.example.pkg1;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
class GfG
{
/*inserts an element x to the set s */
void insert(Set<Integer> s, int x)
{
// Your code here
s.add(x);
}
/*prints the contents of the set s in ascending order */
void print_contents(Set<Integer> s)
{
// Your code here
List<Integer> list = new ArrayList<Integer>(s);
Collections.sort(list);
for(Integer i : list){
System.out.print(i + " ");
}
}
/*erases an element x from the set s */
void erase(Set<Integer> s, int x)
{
// Your code here
s.remove(x);
}
/*returns the size of the set s */
int size(Set<Integer> s)
{
// Your code here
return s.size();
}
/*returns 1 if the element x is
present in set s else returns -1 */
int find(Set<Integer> s, int x)
{
// Your code here
if(s.contains(x)) {
return 1;
}
else {
return -1;
}
}
}
public class SetExample1 {
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t>0)
{
Set<Integer> s = new HashSet<Integer>() ;
int q = sc.nextInt();
while(q>0)
{
GfG g = new GfG();
char c = sc.next().charAt(0);
if(c == 'a')
{
int x = sc.nextInt();
g.insert(s,x);
}
if(c =='b')
{
g.print_contents(s);
}
if(c == 'c')
{
int x = sc.nextInt();
g.erase(s,x);
}
if(c == 'd')
{
int x = sc.nextInt();
System.out.print(g.find(s,x)+" ");
}
if(c == 'e')
System.out.print(g.size(s)+" ");
q--;
//System.out.println();
}
t--;
System.out.println();
}
}
}