Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rukas90 · Nov 29, 2018 at 12:22 AM · liststringduplicatelinqregex

LINQ Find String Duplicates in List

I am trying to prevent name duplicates in a List, but with no luck so far.

I have a list of entries and each entry has a name (e.g. entries: "file", "file1", "someFile", "anotherFile"). Whenever I create new entry I add it to the entry List. But I don't want to add new entry with the same name.

I have a file that I just created (e.g. name: "file"). How do I find all name duplicates and make it something like this at the end: "file2"? Sorry if the question is a bit vague.

I tried to use LINQ and Regex, but I'm kind of new to those things so not sure what I'm doing..

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Arthur_Kenichi · Nov 29, 2018 at 01:10 AM

If you don't wanna add a duplicate in the first place (or if you want to have duplicates discarded when trying to add them), you can use the C# HashSet Class:

"The HashSet class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order."

HashSet.Add(T) Method: "Returns Boolean true if the element is added to the HashSet object; false if the element is already present."

Then if the Add method returns false, you can try to add another name.


If you want to check all the other values with LINQ, there are a lot of examples in the internet, I'll find one now for you: C# LINQ find duplicates in List "Using LINQ, from a List, how can I retrieve a list that contains entries repeated more than once and their values?[...] The easiest way to solve the problem is to group the elements based on their value, and then pick a representative of the group if there are more than one element in the group. In LINQ, this translates to:

 var query = lst.GroupBy(x => x)
               .Where(g => g.Count() > 1)
               .Select(y => y.Key)
               .ToList();

"

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Arthur_Kenichi · Nov 29, 2018 at 01:28 AM 0
Share

I'm a little bit off of LINQ too, but from what I understand: GroupBy(x => x) means "create sub 'lists' of the same object"; then Where(g => g.Count() > 1) discards all the "sub lists" which have only one object, which means they didn't repeat values; Select(y => y.$$anonymous$$ey) will get a "list" (Enumerable) of the "$$anonymous$$eys" that represent the each of the ''sub lists" which have repeated values from Where. That means the item repeating in each group from GroupBy(x => x) that had Count > 1. And finally .ToList(); turns Enumerable into List again, as each LINQ function turned the List into a Enumerable Class.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

98 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Remove duplicates vector y from vector3 list 1 Answer

A node in a childnode? 1 Answer

How to Detect Touch on a Random String from a List? 1 Answer

NullReferenceException when adding String to list 1 Answer

How to replace a word inside a string with different white space combos? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges