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 8009133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:21:19+00:00 2026-06-04T18:21:19+00:00

I have a List(of String). For example: {C1, C12, C10, C1, C6, C22, C1,

  • 0

I have a List(of String). For example: {“C1”, “C12”, “C10”, “C1”, “C6”, “C22”, “C1”, “C6”}. I am trying to write a function to give me a list of duplicates: {“C1”, “C6”} in the list. Each duplicate will be listed only once. The function I wrote does give me anything back at all. I can’t figure out why. Any help or alternative approaches are appreciated. FYI, I see a very similar question in C# but I don’t know how to translate that syntax into VB.net since I am not up to speed on LINQ yet. It is here: How to get duplicate items from a list using LINQ?

    ''' <summary>
    ''' Given a List(Of String), returns a list of items that are duplicated in the list.
    ''' Each duplicate returned is unique.
    ''' </summary>
    ''' <param name="Set1"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetDuplicateItems(ByVal Set1 As List(Of String)) As List(Of String)
        Dim DistinctItems As IEnumerable(Of String)
        'Dim DistinctResults As New List(Of String)
        Dim DuplicateItems As IEnumerable(Of String)
        Dim ItemsToReturn As New List(Of String)

        'Get a set of unique items in the list
        DistinctItems = Set1.Select(Function(x) x).Distinct()
        'Do I need to enumerate the result in order to force the thing to execute?
        'See remarks section of http://msdn.microsoft.com/en-us/library/bb300779.aspx
        'For Each Item As String In DistinctItems
        '    DistinctResults.Add(Item)
        'Next
        'Do a set subtraction (Set1 - UniqueItems)
        DuplicateItems = Set1.Except(DistinctItems)

        For Each Item As String In DuplicateItems
            ItemsToReturn.Add(Item)
        Next

        Return ItemsToReturn
    End Function
  • 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-06-04T18:21:21+00:00Added an answer on June 4, 2026 at 6:21 pm

    This has nothing to do with deferred execution, your algorithm is just wrong: Distinct does not return the unique items of the list, it just removes duplicates. Example: {"C1", "C12", "C10", "C1", "C6", "C22", "C1", "C6"}.Distinct() yields {"C1", "C12", "C10", "C6", "C22"} in your case. Thus, Set1.Except(DistinctItems) will always result in an empty list.


    Here’s an alternative solution to your problem. It selects all items whose count in the list is greater than one:

    Dim duplicates = list.Where(Function(x) list.Where(Function(y) x = y).Count() > 1).Distinct()
    

    Usage example:

    Dim list As New List(Of String) From {"a", "a", "b", "c", "c"}
    Dim duplicates = list.Where(Function(x) list.Where(Function(y) x = y).Count() > 1).Distinct()
    ' duplicates now contains {"a", "c"}
    

    EDIT: Alternative solution using GroupBy (inspired by aquinas):

    Dim duplicates = list.GroupBy(Function(x) x).Where(Function(x) x.Count > 1).Select(Function(x) x.Key)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file with a list of string (one cloumn). File example sdfsdfsdf
I have a list of objects each with a child list of string. I
For example, suppose I have val list: List[(String, Double)] with values 04-03-1985, 1.5 05-03-1985,
For example, I have a variable $(modules), and this variable contains a list (string
Example of the problem If I have a list of valid option strings which
I have List[(String,String)] and I need to sort them by the 2nd value and
I have list of string which I want to add as option in select
I have a list of string values that I want add to a hashtable
I have a list of string like: s = [(abc,bcd,cde),(123,3r4,32f)] Now I want to
I have a list: List(Of String) I want to filter it with LINQ. I

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.