#1.Create a set from elements with duplicates
arr=(0 2 4 6 8 2 4 6)
declare -A set1
for i in "${arr[@]}"; do set1[$i]=1; done
#2.Print the set
# use 'declare -p' to print
declare -p set1
# or use for loop
for i in "${!set1[@]}"; do echo -n "$i "; done; echo
#3.Check the membership of '0'
[ -n "${set1[0]}" ] && echo "found 0 in set"
-------output----------
#1.Create a set from elements with duplicates
#2.Print the set
# use 'declare -p' to print
declare -A set1='([0]="1" [2]="1" [4]="1" [6]="1" [8]="1" )'
# or use for loop
0 2 4 6 8
#3.Check the membership of '0'
found 0 in set
|
use feature ":5.10";
#1.Create a set from elements with duplicates
@lst=(0,2,4,6,8,2,4,6);
%set1 = map { $_ => 1 } @lst;
#2.Print the set
say keys %set1;
#3.Check the membership of '0'
$set1{0} and say "found 0 in set";
-------output----------
#1.Create a set from elements with duplicates
#2.Print the set
60284
#3.Check the membership of '0'
found 0 in set
|
#1.Create a set from elements with duplicates
set1 = set([0,2,4,6,8,2,6])
#2.Print the set
print set1
# or use curly braces to define
set2 = {0,2,4,6,8,2,6}
print set2
#3.Check the membership of '0'
if 0 in set1: print "found 0 in set"
-------output----------
#1.Create a set from elements with duplicates
#2.Print the set
set([0, 8, 2, 4, 6])
# or use curly braces to define
set([8, 0, 2, 4, 6])
#3.Check the membership of '0'
found 0 in set
|