Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6676399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:59:00+00:00 2026-05-26T03:59:00+00:00

The below code is list element. List <string> lsLinks = new List<string>(); Before adding

  • 0

The below code is list element.

List <string> lsLinks = new List<string>();

Before adding new strings i want to check whether list is containing the string i am going to add or not. How can i do that with most efficient way.

I can iterate through whole list and check but i think that would not be performance wise.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:59:00+00:00Added an answer on May 26, 2026 at 3:59 am

    The most efficient way would be to simply use a HashSet<T>, either instead of the list (if order doesn’t matter), or in addition to the list, if it does.

    i.e. either

    HashSet<string> lsLinks = new HashSet<string>();
    // now just Add() all you like; only ever one of each, but order is not defined
    

    or

    List<string> lsLinks = new List<string>();
    HashSet<string> unique = new HashSet<string>();
    // now, when needed, check if the item is new to "unique"
    if(unique.Add(newValue)) lsLinks.Add(newValue);
    

    You might also find a use for .Distinct() in LINQ, i.e.

    var uniqueList = someSourse.Distinct().ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The below code works when there are a few element in the To list
I got below mentioned code for adding my application to Windows Firewall Exception list.
I have the below code, which iterates over a list based on a custom
I am actually having a list of text boxes. I am using below code
In the code below i`m trying to read a list of selected projects and
I have the code below for getting a list of child processes on windows
Given the code below, how do I compare a List of objects's values with
Below is the code that I'm using in ms access to list the data
I met an interesting issue about C#. I have code like below. List<Func<int>> actions
Let's say I have a bean like below. class Customer{ private String code; private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.