Answers for "Set"

22

python sets

# You can't create a set like this in Python
my_set = {} # ---- This is a Dictionary/Hashmap

# To create a empty set you have to use the built in method:
my_set = set() # Correct!


set_example = {1,3,2,5,3,6}
print(set_example)

# OUTPUT
# {1,3,2,5,6} ---- Sets do not contain duplicates and are unordered
Posted by: Guest on March-15-2020
4

sets in python

basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(basket)                      # show that duplicates have been removed
# OUTPUT {'orange', 'banana', 'pear', 'apple'}
print('orange' in basket)                 # fast membership testing
# OUTPUT True
print('crabgrass' in basket)
# OUTPUT False

# Demonstrate set operations on unique letters from two words

print(a = set('abracadabra'))
print(b = set('alacazam'))
print(a)                                  # unique letters in a
# OUTPUT {'a', 'r', 'b', 'c', 'd'}
print(a - b)                             # letters in a but not in b
# OUTPUT {'r', 'd', 'b'}
print(a | b)                              # letters in a or b or both
# OUTPUT {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
print(a & b)                              # letters in both a and b
# OUTPUT {'a', 'c'}
print(a ^ b)                              # letters in a or b but not both
# OUTPUT {'r', 'd', 'b', 'm', 'z', 'l'}
Posted by: Guest on March-04-2021
2

set

SET: Can only store unique values, 
     And does not maintain order
- HashSet can have null, order is not guaranteed
- LinkedHashSet can have null and keeps the order 
- TreeSet sorts the order and don't accept null
Posted by: Guest on January-06-2021
0

how to define a set

emptySet = set()
Posted by: Guest on May-10-2021
0

set

@echo off 
set a[0]=1 
set a[1]=2  
set a[2]=3 
Rem Setting the new value for the second element of the array 
Set a[1]=5 
echo The new value of the second element of the array is %a[1]%
Posted by: Guest on June-29-2021
0

Set

Set<String> names = new HashSet<>();
names.add("John");
names.add("Jack");
names.add("John");
System.out.println(names); // [John, Jack]

names.remove("John");

boolean contains = names.contains("Jack"); // true

for (String name: names) {
    System.out.println(name);
}
Posted by: Guest on April-25-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language