Given a Set, the task is to remove the last element from this Set in C++.
Examples:
Input: set = [10 20 30 70 80 90 100 40 50 60],
valueOfElementToBeDeleted = 100
Output: 10 20 30 40 50 60 70 80 90
Input: set = [1 2 3 4 5],
valueOfElementToBeDeleted = 3
Output: 1 2 4 5
Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element.
Approach: In this method, the last element is deleted by using the erase function and calling it with the value of the last element as its argument. If the value of the last element is not known, then use the previous method.
Syntax:
size_type erase (const value_type& valueOfElementToBeDeleted);
Below is the implementation of the above approach:
C++
#include <iostream>
#include <set>
using namespace std;
void printSet(set< int > myset)
{
set< int >::iterator it;
for (it = myset.begin(); it != myset.end(); ++it)
cout << ' ' << *it;
cout << '\n' ;
}
void deleteByValue(set< int > myset,
int valueOfElementToBeDeleted)
{
cout << "\nSet originally: " ;
printSet(myset);
myset.erase(valueOfElementToBeDeleted);
cout << "Set after deleting "
<< valueOfElementToBeDeleted
<< ": " ;
printSet(myset);
}
int main()
{
set< int > myset;
for ( int i = 1; i < 10; i++)
myset.insert(i * 10);
int valueOfElementToBeDeleted = 50;
deleteByValue(myset, valueOfElementToBeDeleted);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static void printSet(TreeSet<Integer> myset)
{
for ( int it : myset)
{
System.out.print(it + " " );
}
System.out.println();
}
static void deleteByValue(TreeSet<Integer> myset,
int valueOfElementToBeDeleted)
{
System.out.print( "\nSet originally: " );
printSet(myset);
myset.remove(valueOfElementToBeDeleted);
System.out.print( "Set after deleting " +
valueOfElementToBeDeleted + ": " );
printSet(myset);
}
public static void main (String[] args)
{
TreeSet<Integer> myset = new TreeSet<Integer>();
for ( int i = 1 ; i < 10 ; i++)
{
myset.add(i * 10 );
}
int valueOfElementToBeDeleted = 50 ;
deleteByValue(myset, valueOfElementToBeDeleted);
}
}
|
Python3
def printset(myset):
for i in myset:
print (i, end = " " )
print ()
def deleteByValue(myset, value):
myset = sorted (myset)
print ( "Set originally:" , end = " " )
printset(myset)
myset.remove(value)
print ( "Set after deleting" , value,
":" , end = " " )
printset(myset)
myset = set ()
for i in range ( 1 , 10 ):
myset.add(i * 10 )
value = 50
deleteByValue(myset, value);
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static void printSet(HashSet< int > myset)
{
foreach ( int it in myset)
{
Console.Write(it + " " );
}
Console.WriteLine();
}
static void deleteByValue(HashSet< int > myset, int valueOfElementToBeDeleted)
{
Console.Write( "\nSet originally: " );
printSet(myset);
myset.Remove(valueOfElementToBeDeleted);
Console.Write( "Set after deleting " + valueOfElementToBeDeleted + ": " );
printSet(myset);
}
static public void Main ()
{
HashSet< int > myset = new HashSet< int >();
for ( int i = 1; i < 10; i++)
{
myset.Add(i * 10);
}
int valueOfElementToBeDeleted = 50;
deleteByValue(myset, valueOfElementToBeDeleted);
}
}
|
Javascript
<script>
function printSet(myset)
{
for (let it of myset.values())
{
document.write(it + " " );
}
document.write( "<br>" );
}
function deleteByValue(myset, valueOfElementToBeDeleted)
{
document.write( "<br>Set originally: " );
printSet(myset);
myset. delete (valueOfElementToBeDeleted);
document.write( "Set after deleting " +
valueOfElementToBeDeleted + ": " );
printSet(myset);
}
let myset = new Set();
for (let i = 1; i < 10; i++)
{
myset.add(i * 10);
}
let valueOfElementToBeDeleted = 50;
deleteByValue(myset, valueOfElementToBeDeleted);
</script>
|
Output:
Set originally: 10 20 30 40 50 60 70 80 90
Set after deleting 50: 10 20 30 40 60 70 80 90
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
27 Jan, 2022
Like Article
Save Article