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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:04:11+00:00 2026-05-30T21:04:11+00:00

I would like to remove the duplicate elements from a List. Some elements of

  • 0

I would like to remove the duplicate elements from a List. Some elements of the list looks like this:

Book  23
Book  22
Book  19
Notebook 22
Notebook 19
Pen 23
Pen 22
Pen 19

To get rid of duplicate elements i’ve done this:

List<String> nodup = dup.Distinct().ToList();

I would like to keep in the list just

Book 23
Notebook 22
Pen 23

How can i do that ?

  • 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-30T21:04:12+00:00Added an answer on May 30, 2026 at 9:04 pm

    You can use LINQ in combination with some String operations to group all your itemy by name and MAX(Number):

    var q = from str in list
            let Parts = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
            let item = Parts[ 0 ]
            let num = int.Parse(Parts[ 1 ])
            group new  { Name = item, Number = num } by item into Grp
            select new {
                Name  = Grp.Key,
                Value = Grp.Max(i => i.Number).ToString()
            };
    
    var highestGroups = q.Select(g => 
        String.Format("{0} {1}", g.Name, g.Value)).ToList();
    

    (Same as Reed’s approach but in query syntax which is better readable to my mind)

    Edit: I cannot reproduce your comment that it does not work, here is sample data:

    List<String> list = new List<String>();
    list.Add("Book  23");
    list.Add("Book  22");
    list.Add("Book 19");
    list.Add("Notebook  23");
    list.Add("Notebook  22");
    list.Add("Notebook  19");
    list.Add("Pen  23");
    list.Add("Pen  22");
    list.Add("Pen  19");
    list.Add("sheet 3");
    
    var q = from str in list
            let Parts = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
            let item = Parts[ 0 ]
            let num = int.Parse(Parts[ 1 ])
            group new  { Name = item, Number = num } by item into Grp
            select new {
                Name  = Grp.Key,
                Value = Grp.Max(i => i.Number).ToString()
            };
    
    var highestGroups = q.Select(g => String.Format("{0} {1}", g.Name, g.Value));
    MessageBox.Show(String.Join(Environment.NewLine, highestGroups));
    

    The result:

    Book 23
    Notebook 23
    Pen 23
    sheet 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to remove duplicate entries from a file. The file looks like
I'm working on a Java library and would like to remove some functions from
I would like to know an efficient method to remove duplicate items from a
Possible Duplicate: Remove from the array elements that are repeated. in Ruby I would
I would like to remove the domain/computer information from a login id in C#.
I would like to remove two files from a folder at the conclusion of
I would like to remove selected commit log entries from a linear commit tree,
I would like to remove some warnings for some classes by generating an automatic
I would like to remove a trailing slash from a string. For example if
I would like to remove an old JPanel from the Window (JFrame) and add

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.