Sets

A Set is a data structure in programming that represents a collection of unique elements. Unlike arrays or lists, Sets do not allow duplicate elements, and they typically do not maintain any particular order of elements. Sets are useful in scenarios where uniqueness of elements is important, such as removing duplicates from a list, checking for membership, or performing set operations like union, intersection, and difference. Unfortunately, not all languages have built in support for Sets especially for lower level languages like C. In those cases, it might be possible to build them from the ground up using other data structures like structs.

Select Languages

Examples

C

// no built in implementation

C#

using System.Collections.Generic;

HashSet<string> cars = new HashSet<string>();

cars.Add("Mazda");
cars.Add("Toyota");
cars.Add("Honda");

Console.WriteLine(cars.Contains("Honda") + "\n");

cars.Remove("Honda");
Console.WriteLine(cars.Contains("Honda"));

C++

#include <iostream>
#include <set>
using namespace std;

set<string> cars;
cars.insert("Mazda");
cars.insert("Toyota");
cars.insert("Honda");

bool result1 = 
  cars.find("Honda") == cars.end() ?
  false: true;
cout << result1 << endl;

cars.erase("Honda");

bool result2 = 
  cars.find("Honda") == cars.end() ?
  false: true;
cout << result2 << endl;

Go

// no built in implementation

Java

import java.util.*;

Set<String> cars = new HashSet<String>();
cars.add("Mazda");
cars.add("Toyota");
cars.add("Honda");

System.out.println(cars.contains("Honda") );

cars.remove("Honda");
System.out.println(cars.contains("Honda"));

JavaScript

const cars = new Set();

cars.add("Mazda");
cars.add("Toyota");
cars.add("Honda");

console.log(cars.has("Honda")); 

cars.delete("Honda");
console.log(cars.has("Honda")); 

Kotlin

val cars = hashSetOf<String>();
cars.add("Mazda");
cars.add("Toyota");
cars.add("Honda");
println(cars.contains("Honda"));

cars.remove("Honda");
println(cars.contains("Honda"));

MatLab

# no built in implementation
# use external libraries

PHP

// No Native Support or Implementation
// Use PECL and DS library

Python

cars = set();

cars.add('Mazda')
cars.add('Toyota')
cars.add('Honda')

print('Honda' in cars)

cars.remove('Honda')
print('Honda' in cars)

R

# no built in implementation
# use external libraries

Ruby

require 'set'

cars = Set.new
cars.add("Mazda")
cars.add("Toyota")
cars.add("Honda")

puts cars.include?("Honda")

cars.delete("Honda")
puts cars.include?("Honda")

Rust

use std::collections::HashSet;

cars.insert("Mazda");
cars.insert("Toyota");
cars.insert("Honda");

println!("{}", cars.contains("Honda"));

cars.remove("Honda");
println!("{}", cars.contains("Honda"));

Scala

var cars:Set[String] = Set();

cars += "Mazda";
cars += "Toyota";
cars += "Honda";

println(cars.contains("Honda")); 

cars -= "Honda";
println(cars.contains("Honda"));

Swift

var cars = Set<String>();

cars.insert("Mazda");
cars.insert("Toyota");
cars.insert("Honda");

print(cars.contains("Honda")); 

cars.remove("Honda");
print(cars.contains("Honda")); 

TypeScript

const cars:Set<string> = new Set();

cars.add("Mazda");
cars.add("Toyota");
cars.add("Honda");

console.log(cars.has("Honda")); 

cars.delete("Honda");
console.log(cars.has("Honda"));

Copyright 2025. All Rights Reserved. IronCodeMan.