स्विफ्ट सेट: इसका उपयोग कैसे करें और क्यों करें? (उदाहरण के साथ)

इस ट्यूटोरियल में, आप सेट के बारे में, सेट बनाना, उन्हें संशोधित करना और सेटों में कुछ सामान्य ऑपरेशन के बारे में जानेंगे।

पिछले स्विफ्ट Arrays लेख में, हमने एक ऐसी सरणी बनाने के बारे में सीखा जो एक ऑर्डर की गई सूची में कई मान रख सकती है।

लेकिन, अगर हमें यह सुनिश्चित करना है कि एक सूची केवल एक बार एक मूल्य पकड़ सकती है, तो हम स्विफ्ट में एक सेट का उपयोग करते हैं।

एक सेट क्या है?

सेट बस एक कंटेनर होता है जो एक अनियोजित सूची में कई प्रकार के डेटा प्रकारों को पकड़ सकता है और कंटेनर में अद्वितीय तत्व सुनिश्चित करता है (अर्थात प्रत्येक डेटा केवल एक बार दिखाई देता है)।

अनऑर्डर की गई सूची का अर्थ है कि आपको उसी क्रम में तत्व नहीं मिलेंगे जैसे आपने सेट में वस्तुओं को परिभाषित किया था।

सरणियों पर सेट का उपयोग करने का मुख्य लाभ यह है कि आपको यह सुनिश्चित करने की आवश्यकता है कि एक आइटम केवल एक बार दिखाई देता है और जब वस्तुओं का क्रम महत्वपूर्ण नहीं होता है।

एक सेट में संग्रहित मूल्य हैज़ेबल होना चाहिए । इसका मतलब है कि उसे हैशवेल प्रॉपर्टी देनी होगी। यह महत्वपूर्ण है क्योंकि सेट अनियंत्रित हैं और इसका उपयोग हैशवैल्यू का उपयोग सेट के तत्वों तक पहुंचने के लिए किया जाता है।

स्विफ्ट की बुनियादी प्रकार के सभी (जैसे String, Int, Double, और Bool) डिफ़ॉल्ट रूप से hashable हैं, और निर्धारित मूल्य प्रकार के रूप में इस्तेमाल किया जा सकता। हालाँकि, आप स्विफ्ट में अपना हसबिश टाइप भी बना सकते हैं जिसे एक सेट में संग्रहित किया जा सकता है।

स्विफ्ट में सेट कैसे घोषित करें?

आप प्रकार को निर्दिष्ट करके एक खाली सेट बना सकते हैं जैसा कि डेटा के प्रकार द्वारा सेट किया गया है जिसे वह अपने भीतर संग्रहीत कर सकता है।

उदाहरण 1: एक खाली सेट की घोषणा करना

 let emptyIntSet:Set = () print(emptyIntSet) 

या

 let emptyIntSet:Set = Set() print(emptyIntSet) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 ()

उपरोक्त कार्यक्रम में, हमने एक प्रकार के निरंतर रिक्त स्थान की घोषणा की है Setजो पूर्णांक के कई मानों को संग्रहीत कर सकता है और 0 मानों के साथ प्रारंभ किया जा सकता है।

चूंकि, स्विफ्ट एक प्रकार की अनुमान भाषा है, आप डेटा प्रकार को निर्दिष्ट किए बिना सीधे सेट भी बना सकते हैं, लेकिन कुछ मानों के साथ आरंभ करना होगा ताकि कंपाइलर इसके प्रकार का अनुमान लगा सके:

उदाहरण 2: कुछ मूल्यों के साथ एक सेट की घोषणा करना

 let someIntSet:Set = (1, 2, 3, 4, 5, 6, 7, 8, 9) print(someIntSet) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 (2, 4, 9, 5, 6, 7, 3, 1, 8)

उपरोक्त कार्यक्रम में, हमने एक निरंतर someIntSet घोषित किया है जो स्पष्ट रूप से प्रकार निर्दिष्ट किए बिना इंटेगर के सेट को स्टोर कर सकता है। हालांकि, :Setचर को परिभाषित करते समय हमें लिखना होगा, अन्यथा स्विफ्ट हमारे लिए एक सरणी बनाएगा।

इसके अलावा, सरणियों के रूप में, हमने सेट को 1, 2, 3, 4, 5, 6, 7, 8, 9 मानों के साथ उपयोग किया ()है।

जैसा कि आपने सीखा है, जब आप सेट के अंदर के मानों को प्रिंट करने का प्रयास करते हैं, तो आपको print(someIntSet)परिणाम एक अलग क्रम में मिलेंगे, क्योंकि आपने सेट में मौजूद वस्तुओं को परिभाषित किया है क्योंकि यह बिना किसी निर्धारित क्रम के मूल्य को संग्रहीत करता है। इसलिए, हर बार जब आप ऑर्डर में बदलाव करते हैं तो पहुंच जाते हैं।

उदाहरण 3: डुप्लिकेट मानों के साथ एक सेट की घोषणा करना

 let someStrSet:Set = ("ab","bc","cd","de","ab") print(someStrSet) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 ("डे", "एब", "सीडी", "बीसी")

उपरोक्त कार्यक्रम में, हमने सेट में एक डुप्लिकेट मान एब परिभाषित किया है । तथा। जब हम सेट के अंदर मूल्य का उपयोग करने का प्रयास करते हैं print(someStrSet), तो डुप्लिकेट मान स्वचालित रूप से सेट से हटा दिया जाता है। इसलिए, सेट इसके अंदर अद्वितीय तत्वों / मूल्यों की गारंटी देता है।

आप स्विफ्ट में अपने स्वयं के कस्टम हैश प्रकार के साथ एक सेट भी घोषित कर सकते हैं। अधिक जानने के लिए, स्विफ्ट हस्ब्री पर जाएं।

स्विफ्ट में सेट तत्वों का उपयोग कैसे करें?

आप सरणियों के रूप में सबस्क्रिप्ट सिंटैक्स का उपयोग करके सेट के तत्वों तक नहीं पहुंच सकते। ऐसा इसलिए है क्योंकि सेट अनियंत्रित हैं और तत्वों तक पहुंचने के लिए सूचक नहीं हैं।

तो, आपको इसकी विधियों और गुणों का उपयोग करके या फॉर-इन लूप का उपयोग करके सेट तक पहुंचने की आवश्यकता है।

उदाहरण 4: एक सेट के तत्वों तक पहुँचना

 var someStrSet:Set = ("ab", "bc", "cd", "de") for val in someStrSet ( print(val) ) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 de ab cd बी.सी. 

उपरोक्त कार्यक्रम में, हमें सेट के तत्वों की तुलना में अलग-अलग क्रम में घाटी मिलती है क्योंकि सेट सरणियों के विपरीत अनियंत्रित होते हैं।

आप नीचे दिए गए सेट से सीधे मूल्य को हटाकर एक सेट के तत्व तक भी पहुँच सकते हैं:

उदाहरण 5: निकालें () का उपयोग करके सेट के तत्वों तक पहुँचना

 var someStrSet:Set = ("ab", "bc", "cd", "de") let someVal = someStrSet.remove("cd") print(someVal) print(someStrSet) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 वैकल्पिक ("सीडी") ("डी", "एबी", "बीसी") 

उपरोक्त कार्यक्रम में, आप हटा सकते हैं विधि एक वैकल्पिक स्ट्रिंग लौटाते हैं। इसलिए, आपको नीचे दिए अनुसार वैकल्पिक हैंडलिंग करने की सलाह दी जाती है। वैकल्पिक के बारे में अधिक जानने के लिए, स्विफ्ट वैकल्पिक पर जाएँ।

उदाहरण 6: हटाने के लिए वैकल्पिक हैंडलिंग ()

 var someStrSet:Set = ("ab", "bc", "cd", "de") if let someVal = someStrSet.remove("cd") ( print(someVal) print(someStrSet) ) else ( print("cannot find element to remove") ) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 सीडी ("डी", "एब", "बीसी") 

एक सेट में नया तत्व कैसे जोड़ें?

आप insert()स्विफ्ट में विधि का उपयोग करके सेट में एक नया तत्व जोड़ सकते हैं ।

उदाहरण 7: डालें का उपयोग करके नया तत्व जोड़ें ()

 var someStrSet:Set = ("ab", "bc", "cd", "de") someStrSet.insert("ef") print(someStrSet) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 ("ab", "de", "cd", "ef", "bc")

In the above program, we used the set's insert() method to add a new element to a set. Since, sets are unordered, the position of the inserted element isn't known.

Set Operations

Another main advantage of using Sets is you can perform set operations such as combining two sets together, determining which values two sets have in common etc. This operations are similar to the Set operation in Mathematics.

1. Union

The union of two sets a and b is the set of elements which are in a, or b, or in both a and b.

 let a: Set = (1, 3, 5, 7, 9) let b: Set = (0, 2, 4, 6, 8) print(a.union(b)) 

When you run the above program, the output will be:

 (8, 2, 9, 4, 5, 7, 6, 3, 1, 0)

2. Intersection

The intersection of two sets a and b is the set that contains all elements of a that also belong to b.

 let a: Set = (1, 3, 5, 7, 9) let b: Set = (0, 3, 7, 6, 8) print(a.intersection(b)) 

When you run the above program, the output will be:

 (7, 3)

Therefore, print(a.intersection(b)) outputs a new set with values (7, 3) that are common in both a and b.

3. Subtracting

The subtraction of two sets a and b is the set that contains all elements of a but removing the elements that also belong to b.

 let a: Set = (1, 3, 5, 7, 9) let b: Set = (0, 3, 7, 6, 8) print(a.subtracting(b)) 

When you run the above program, the output will be:

 (5, 9, 1)

Therefore, print(a.subtracting(b)) outputs a new set with values (5, 9, 1).

4. Symmetric Difference

The symmetric difference of two sets a and b is the set that contains all elements which are in either of the sets but not in both of them.

 let a: Set = (1, 3, 5, 7, 9) let b: Set = (0, 3, 7, 6, 8) print(a.symmetricDifference(b)) 

When you run the above program, the output will be:

 (5, 6, 8, 0, 1, 9)

Therefore, print(a.symmetricDifference(b)) outputs a new set with values (5, 6, 8, 0, 1, 9).

Set Membership and Equality Operations

Set Equality

You can use == operator to check whether two sets contains same elements or not. It returns true if two sets contains same elements otherwise returns false.

Example 5: Set equality operations

 let a: Set = (1, 3, 5, 7, 9) let b: Set = (0, 3, 7, 6, 8) let c:Set = (9, 7, 3, 1, 5) if a == b ( print("a and b are same") ) else ( print("a and b are different") ) if a == c ( print("a and c are same") ) else ( print("a and c are different") ) 

When you run the above program, the output will be:

 a and b are different a and c are same

Set membership

You can also check relationship between two sets using the following methods:

  • isSubset(of:)This method determines whether all of the values of a set are contained in the specified set.
  • isSuperset(of:) This method determines whether a set contains all of the values in a specified set
  • isStrictSubset(of:) or isStrictSuperset(of:): This method determines whether a set is a subset or superset, but not equal to, a specified set.
  • isDisjoint(with:) This method determines whether two sets have no values in common.

Example 6: Set membership operations

 let a: Set = (1, 3, 5, 7, 9) let b: Set = (0, 3, 1, 7, 6, 8, 9, 5) print("isSubset:", a.isSubset(of: b)) print("isSuperset:", b.isSuperset(of: a)) print("isStrictSubset:", a.isStrictSubset(of: b)) print("isDisjointWith:", a.isDisjoint(with: b)) 

When you run the above program,the output will be:

 isSubset: true isSuperset: true isStrictSubset: true isDisjointWith: false 

Let's analyze methods used inside the print statement below:

  • isSubsetreturns true because the set b contains all the elements in a
  • isSupersetreturn true because b contains all of the values of a.
  • isStrictSubsetreturns true because set b contains all the element in a and both sets are not equal.
  • isDisjointWithreturns false because a and b have some values in common.

Some helpful built in Set functions & properties

1. isEmpty

This property determines if a set is empty or not. It returns true if a set does not contain any value otherwise returns false.

Example 7: How isEmpty works?

 let intSet:Set = (21, 34, 54, 12) print(intSet.isEmpty) 

When you run the program, the output will be:

 false

2. first

This property is used to access first element of a set.

Example 8: How first works?

 let intSet = (21, 34, 54, 12) print(intSet.first) 

When you run the program, the output will be:

 Optional(54)

Since set is an unordered collection, the first property does not guarantee the first element of the set. You may get other value than 54.

Similarly, you can use last property to access last element of a set.

3. insert

The insert function is used to insert/append element in the set.

Example 9: How insert works?

 var intSet:Set = (21, 34, 54, 12) intSet.insert(50) print(intSet) 

When you run the program, the output will be:

 (54, 12, 50, 21, 34)

4. reversed

This function returns the elements of a set in reverse order.

Example 10: How reversed() works?

 var intSet:Set = (21, 22, 23, 24, 25) print(intSet) let reversedSet = intSet.reversed() print(reversedSet) 

When you run the program, the output will be:

 (22, 23, 21, 24, 25) (25, 24, 21, 23, 22) 

5. count

This property returns the total number of elements in a set.

Example 11: How count works?

 let floatSet:Set = (10.2, 21.3, 32.0, 41.3) print(floatSet.count) 

When you run the program, the output will be:

 4

6. removeFirst

This function removes and returns the first value from the set.

Example 12: How removeFirst works?

 var strSet:Set = ("ab", "bc", "cd", "de") let removedVal = strSet.removeFirst() print("removed value is (removedVal)") print(strSet) 

When you run the program, the output will be:

 removed value is de ("ab", "cd", "bc") 

इसी तरह, आप removeAllकिसी सेट को खाली करने के लिए फ़ंक्शन का भी उपयोग कर सकते हैं ।

दिलचस्प लेख...