#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int> s1; // Declaring set
// inserting elements in set
s1.insert(10);
s1.insert(5);
s1.insert(12);
s1.insert(4);
// printing elements of set
for (auto i : s1) {
cout << i << ' ';
}
cout << endl;
// check if 10 present inside the set
if (s1.count(10) == 1) {
cout << "Element is present in the set:" << endl;
}
// erasing 10 from the set
s1.erase(5);
// printing element of set
for (auto it : s1) {
cout << it << " ";
}
cout << endl;
cout << "Minimum element: " << *s1.begin()
<< endl; // Printing maximum element
cout << "Maximum element: " << *(--s1.end())
<< endl; // Printing minimum element
cout << "Size of the set is: " << s1.size()
<< endl; // Printing the size of the set
return 0;
}
Java
// Java program Illustrating Set Interface
// Importing utility classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of Set and
// declaring object of type String
Set<Integer> hs = new HashSet<Integer>();
// Custom input elements
hs.add(10);
hs.add(5);
hs.add(12);
hs.add(4);
// Print the Set object elements
System.out.println("Set is " + hs);
// Declaring a string
int check = 10;
// Check if the above string exists in
// the SortedSet or not
// using contains() method
System.out.println("Contains " + check + " "
+ hs.contains(check));
// Printing elements of HashSet object
System.out.println(hs);
// Removing custom element
// using remove() method
hs.remove(5);
// Printing Set elements after removing an element
// and printing updated Set elements
System.out.println("After removing element " + hs);
// finding maximum element
Object obj = Collections.max(hs);
System.out.println("Maximum Element = " + obj);
// finding maximum element
Object obj2 = Collections.min(hs);
System.out.println("Maximum Element = " + obj2);
// Displaying the size of the Set
System.out.println("The size of the set is: "
+ hs.size());
}
}
Python
# set of letters
GEEK = {10, 5, 12, 4}
# adding 's'
GEEK.add(15)
print("Letters are:", GEEK)
# adding 's' again
GEEK.add(10)
print("Letters are:", GEEK)
# check if set contain an element
print(5 in GEEK)
# removing an element from set
GEEK.remove(5)
print(GEEK)
# print max element of set
print(max(GEEK))
# print min element of set
print(min(GEEK))
# printing size of the set
print(len(GEEK))
C#
// C# program Illustrating Set Interface
using System;
using System.Collections.Generic;
public class GFG {
public static void Main()
{
HashSet<int> hs
= new HashSet<int>(); // Declaring set
// inserting elements in set
hs.Add(10);
hs.Add(5);
hs.Add(12);
hs.Add(4);
// printing elements of set
foreach(int element in hs)
{
Console.Write(element + " ");
}
Console.WriteLine();
// check if 10 present inside the set
if (hs.Contains(10)) {
Console.WriteLine(
"Element is present in the HashSet");
}
// erasing 10 from the set
hs.Remove(5);
// printing element of set
foreach(int element in hs)
{
Console.Write(element + " ");
}
Console.WriteLine();
int minValue = int.MaxValue;
int maxValue = int.MinValue;
foreach(int element in hs)
{
if (element < minValue) {
minValue = element;
}
if (element > maxValue) {
maxValue = element;
}
}
// Printing minimum element
Console.WriteLine("Minimum element: " + minValue);
// Printing maximum element
Console.WriteLine("Maximum element: " + maxValue);
// Printing the size of the set
Console.WriteLine("Size of the HashSet: "
+ hs.Count);
}
}
JavaScript
// This is the JavaScript code for the above code
const s1 = new Set(); // Declaring set
// inserting elements in set
s1.add(10);
s1.add(5);
s1.add(12);
s1.add(4);
// printing elements of set
for (const i of s1) {
console.log(i);
}
// check if 10 present inside the set
if (s1.has(10)) {
console.log("Element is present in the set:");
}
// erasing 10 from the set
s1.delete(5);
// printing element of set
for (const it of s1) {
console.log(it);
}
console.log("Minimum element: " + Math.min(...s1));
console.log("Maximum element: " + Math.max(...s1));
console.log("Size of the set is: " + s1.size); // Printing the size of
the set
//This code is contributed by sarojmcy2e
Output
4 5 10 12
Element is present in the set:
4 10 12
Minimum element: 4
Maximum element: 12
Size of the set is: 3