File tree Expand file tree Collapse file tree 5 files changed +175
-0
lines changed
01. Data Structures/07. Set Expand file tree Collapse file tree 5 files changed +175
-0
lines changed Original file line number Diff line number Diff line change
1
+ package sets ;
2
+
3
+ import java .util .HashSet ;
4
+ import java .util .Set ;
5
+
6
+ public class MyHashSet {
7
+
8
+ public static void main (String args []) {
9
+
10
+ Set <String > fruits = new HashSet <>();
11
+
12
+ fruits .add ("Apple" );
13
+ fruits .add ("Banana" );
14
+ System .out .println (fruits .add ("Kiwi" )); // this will print true as add function will return true
15
+ System .out .println (fruits .add ("Banana" )); // this will print false as add function will return false
16
+ /*
17
+ * fruits.add("Banana"); this "Banana" will not be added in the set because Set
18
+ * does not allow duplicate elements
19
+ */
20
+
21
+ /*
22
+ * HashSet uses a HashMap internally to implement a Set so the elements will not
23
+ * retain the order in which they are inserted
24
+ */
25
+ System .out .println (fruits );
26
+
27
+ Set <Integer > numbers = new HashSet <>();
28
+ numbers .add (5 );
29
+ numbers .add (3 );
30
+ numbers .add (99 );
31
+ numbers .add (6 );
32
+ numbers .add (5 );
33
+ numbers .add (56 );
34
+ numbers .add (8 );
35
+ numbers .add (1 );
36
+
37
+ System .out .println ("\n NUMBER SET:" );
38
+ System .out .println (numbers );
39
+ }
40
+ }
Original file line number Diff line number Diff line change
1
+ package sets ;
2
+
3
+ import java .util .LinkedHashSet ;
4
+ import java .util .Set ;
5
+
6
+ public class MyLinkedHashSet {
7
+
8
+ public static void main (String [] args ) {
9
+
10
+ Set <String > fruits = new LinkedHashSet <>();
11
+
12
+ fruits .add ("Kiwi" );
13
+ fruits .add ("Banana" );
14
+ fruits .add ("Apple" );
15
+ fruits .add ("Orange" );
16
+ System .out .println (fruits .add ("Banana" )); // this will print false as add function will return false
17
+ /*
18
+ * fruits.add("Banana"); this "Banana" will not be added in the set because Set
19
+ * does not allow duplicate elements
20
+ */
21
+
22
+ /*
23
+ * LinkedHashSet uses a LinkedList internally to implement a Set so the elements
24
+ * will retain the order in which they are inserted
25
+ */
26
+ System .out .println (fruits );
27
+
28
+ Set <Integer > numbers = new LinkedHashSet <>();
29
+ numbers .add (5 );
30
+ numbers .add (3 );
31
+ numbers .add (99 );
32
+ numbers .add (6 );
33
+ numbers .add (5 );
34
+ numbers .add (56 );
35
+ numbers .add (8 );
36
+ numbers .add (1 );
37
+
38
+ System .out .println ("\n NUMBER SET:" );
39
+ System .out .println (numbers );
40
+ }
41
+ }
Original file line number Diff line number Diff line change
1
+ package sets ;
2
+
3
+ import java .util .Set ;
4
+ import java .util .TreeSet ;
5
+
6
+ public class MyTreeSet {
7
+
8
+ public static void main (String [] args ) {
9
+
10
+ Set <String > fruits = new TreeSet <>();
11
+
12
+ fruits .add ("Kiwi" );
13
+ fruits .add ("Banana" );
14
+ fruits .add ("Apple" );
15
+ fruits .add ("Orange" );
16
+ System .out .println (fruits .add ("Banana" )); // this will print false as add function will return false
17
+ /*
18
+ * fruits.add("Banana"); this "Banana" will not be added in the set because Set
19
+ * does not allow duplicate elements
20
+ */
21
+
22
+ /*
23
+ * TreeSet uses a Tree internally to implement a Set so the elements will be
24
+ * arranged in a sorted order, int values will be stored in ascending order,
25
+ * strings will be stored lexicographically
26
+ */
27
+ System .out .println ("FRUIT SET:" );
28
+ System .out .println (fruits );
29
+
30
+ Set <Integer > numbers = new TreeSet <>();
31
+ numbers .add (5 );
32
+ numbers .add (3 );
33
+ numbers .add (99 );
34
+ numbers .add (6 );
35
+ numbers .add (5 );
36
+ numbers .add (56 );
37
+ numbers .add (8 );
38
+ numbers .add (1 );
39
+
40
+ System .out .println ("\n NUMBER SET:" );
41
+ System .out .println (numbers );
42
+ }
43
+ }
Original file line number Diff line number Diff line change
1
+ ### Set is a data structure that is used as a collection of objects. Java supports it through its Java Collection library. There are several ways to implement a set and Java supports three different implementations along with useful operations like intersection between sets.
2
+
3
+
4
+ #### Learn about Set Data Structures in JAVA: [ Video Reference] ( https://youtu.be/rS4VWfPUArY )
5
+ #### [ Article 1] ( https://www.geeksforgeeks.org/set-in-java/ ) | [ Article 2] ( https://iq.opengenus.org/set-in-java/ )
6
+
7
+ <hr >
8
+
9
+ ## HashSet vs TreeSet vs LinkedHashSet:
10
+ ### [ HashSet :] ( ./MyHashSet.java )
11
+ - HashSet Use hashtable to store elements.
12
+ - Elements are not stored in sorted order(unsorted).
13
+ - add,remove,contain method have constant time complexitiy O(1).
14
+
15
+ ### [ TreeSet :] ( ./MyTreeSet.java )
16
+ - TreeSet Use RedBlack tree to store elements.
17
+ - Elements are stored in sorted order (sorted).
18
+ - add, remove, and contains methods has time complexity of O(log (n)).
19
+
20
+ ### [ LinkedHashSet :] ( ./MyLinkedHashSet.java )
21
+ - LinkedHashSet is between HashSet and TreeSet. It is implemented as a hash table with a slinked list running through it.
22
+ - The time complexity of basic methods is O(1).
23
+
24
+ #### HashSet has best-performing implementation of Set compare to LinkedHashSet and TreeSet .
25
+
26
+ ### Problem Link: [ GFG - Subarray with 0 sum] ( https://practice.geeksforgeeks.org/problems/subarray-with-0-sum-1587115621/1# )
27
+ ### Editorial Link: [ Apni Kaksha - Anuj Kumar Sharma] ( https://youtu.be/PSpuM9cimxA )
28
+ ### [ Code] ( ./SubarrayWith0Sum.java )
Original file line number Diff line number Diff line change
1
+ // Problem Link: https://practice.geeksforgeeks.org/problems/subarray-with-0-sum-1587115621/1#
2
+ // Editorial Link: https://youtu.be/PSpuM9cimxA
3
+
4
+ class Solution {
5
+ //Function to check whether there is a subarray present with 0-sum or not.
6
+ static boolean findsum (int arr [],int n )
7
+ {
8
+ Set <Integer > set = new HashSet <>();
9
+ int sum =0 ;
10
+ boolean found = false ;
11
+ for (int ele : arr )
12
+ {
13
+ set .add (sum );
14
+ sum += ele ;
15
+ if (set .contains (sum ))
16
+ {
17
+ found = true ;
18
+ break ;
19
+ }
20
+ }
21
+ return found ;
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments