EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Top Differences Tutorial List vs Set
 

List vs Set

Priya Pedamkar
Article byPriya Pedamkar

Updated March 17, 2023

List-vs-Set

 

 

Introduction to List and Set in C#

List and Set are two of the many data structures supported by C#. The list is an abstract data structure that is linear. It comprises of elements arranged in a linear way. It is possible to add elements to the list at different positions. The list has a property called length (number of elements in the list). In this topic, we are going to learn about List vs Set.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Note: Be careful as lists are different from arrays. The lists have variable size, i.e. elements can be appended in the list while arrays have fixed size.

Set is an abstract data structure. Set has unique elements, and the order of elements is not important. Elements in the set cannot be accessed via indices. The elements themselves are indices in the set, and a particular element can be accessed using a foreach loop.

Head to Head Comparison Between List and Set (Infographics)

Below are the top 5 differences between List vs Set

List-vs-Set-info

Key Differences Between List and Set

Following are the key differences:

  1. The list can contain duplicate elements while set cannot, As a set can have only unique elements. So use a set if you want unique elements.
  2. Elements in the list are ordered while the order is not important in a set, but it depends on the set’s implementation.
  3. Elements are accessed using indices in the list, while elements themselves are indices in the set. Hence foreach loop is used to access elements of the set. However, it can also be used to access the list elements, as shown in the code of the list above.

Let us look at some of the methods of the interfaces of the list and set.

List

Following are some of the methods implemented by the IList interface:

  • int Add(element) – to append the element to the list (at the end) and to return a value indicating successful insertion.
  • void Insert(int, element) – to insert the element at the given position in the list
  • void Clear() – to remove all elements from the list
  • bool Contains(element) – to check if the element is present in the list
  • void Remove(element) – to remove the given element from the list
  • void RemoveAt(int) – to remove the element at the given position
  • int IndexOf(element) – to return the position of the element
  • this[int] – it is an indexer which allows access to the element on the given position

Following is one of the example code for the list:

// C# Program to remove the element at  // the specified index of the List<T>  using System;  using System.Collections.Generic;
class StaticList {
// Main Method      public static void Main(String[] args)
{
// Creating an List<T> of Integers
List<int> firstlist = new List<int>();
// Adding elements to List          firstlist.Add(17);          firstlist.Add(19);          firstlist.Add(21);          firstlist.Add(9);          firstlist.Add(75);          firstlist.Add(19);          firstlist.Add(73);
Console.WriteLine("Elements Present in the List:");
// Displaying the elements of List          for(int k=0;k<firstlist.Count;k++)
{
Console.Write(firstlist[k]+" ");
}
Console.WriteLine(" ");
// removing the element at index 3
Console.WriteLine("Removing the element at index 3");
// 9 will be removed from the List          // and 75 will come at index 3          firstlist.RemoveAt(3);
// Displaying the elements of List          foreach(int k in firstlist)
{
Console.Write(k+" ");
}
}
}

Output:

list vs set output 1

Set

Following are some of the methods implemented by the ISet interface:

  • bool Add(element) – Returns true if the element is added to the set provided it is not present in the set else returns false
  • bool Contains(element) – Returns true if the element is already present in the set, else it returns false
  • bool Remove(element) – Returns true if the element exists in the set and can be removed; else returns false
  • void Clear() – To remove all elements from the set
  • void IntersectWith(Set other) – It finds the intersection of 2 sets (those elements which occur in both the sets), the set on which the method is called and the other set, which is passed as a parameter.
  • void UnionWith(Set other) – It finds the union of 2 sets (all elements in both the sets), the set on which the method is called and the other set, which is passed as a parameter.
  • bool IsSubsetOf(Set other) – Returns true if the set (on which the method is called) is a subset of the other set passed as a parameter, else returns false
  • bool IsSupersetOf(Set other) – Returns true if the set (on which the method is called) is a superset of the other set passed as a parameter, else returns false
  • int Count – Returns the number of elements in the set

Following is one of the example code for the set:

using System;
using System.Collections.Generic;
class HS {
// Driver code
public static void Main()
{
// Creating a HashSet of odd numbers
HashSet<int> odd = new HashSet<int>();
// Inserting elements in HashSet          for (int i = 0; i < 5; i++) {              odd.Add(2 * i + 1);
}
Console.WriteLine("Elements in the HashSet:");         // Displaying the elements in the HashSet
foreach(int i in odd)
{
Console.Write(i+" ");
}
Console.WriteLine("\nTotal elements in the HashSet = "+odd.Count);
Console.WriteLine("Is it possible to remove 7? : "+odd.Remove(7));
Console.WriteLine("New HashSet = ");
foreach(int i in odd)
{
Console.Write(i+" ");
}
}
}

Output:

element in hashset

Comparison Table

The following table illustrates the difference between list and set:

List Set
1. Can contain duplicate elements 1. Elements must be unique
2. Order of elements is important 2. Order of elements is not important, but it depends on the implementation
3. Elements are accessed using index 3. Elements themselves are indices
4. The interface used to implement the list is System.Collections.IList 4. The interface used to implement the set is System.Collections.ISet
5. The list is implemented as a static list (using array) and dynamic list (linked list) 5. Sets are implemented as hashset (hashtable) and sorted set (red-black tree-based)

 

Recommended Articles

This is a guide to List vs Set. Here we have discussed the List vs Set key differences with infographics and comparison table. You may also have a look at the following articles to learn more –

  1. C# List vs Array
  2. C# Array vs List
  3. C# Functions
  4. C# Commands

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW