Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Overcast · May 21, 2013 at 11:35 AM · c#lists

Fastest, cleanest way to find common items in 2 lists

Hi Everyone,

This is a bit of a general programming question sorry. I am just learning C#, and was wondering if someone could help me with getting my head around the most efficient method for handling this with Lists.

I have a list, and I want to compare it to another list and execute some further instruction if both lists have exactly 2 items in common.

I know simple brute ways to do it, but I belief there are some list functions for this sort of thing, and I am trying to expand my capability with C#, rather than relying on old habits.

Thanks for your time

Comment
Add comment · Show 2
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 KiraSensei · May 21, 2013 at 01:10 PM 0
Share

It depends if you test occurs often or not. If not, I think that two "for" loops are good enough. If you launch this test at every frame and the lists are quite long, you may think of using a hash table.

avatar image Overcast · May 21, 2013 at 01:14 PM 0
Share

Thanks for the reply $$anonymous$$iraSensei, I am not calling it too often and decided to use Linq given it's powerful (albeit slightly slower) features. I posted an example below.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Overcast · May 21, 2013 at 01:10 PM

OK, I found an answer, for anyone else interested, I decided to use LINQ. To do so:

Make sure you add it to your script:

 using System.Linq

Then use:

 IEnumerable both = list1.Intersect(list2);
 if(both.Count() == 2){
     //do...
 }
Comment
Add comment · 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
0

Answer by abhinandand · Nov 28, 2017 at 06:30 AM

It is recommended not to use Linq in unity scripting to avoid garbage in the game,It is recommended not to use Linq in Unity scripting to avoid unwanted garbage in the game

Comment
Add comment · 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

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

15 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to check if a value exists in an array (C#) 3 Answers

Dictionary, List, Hashtable - Best Practices 1 Answer

Make Lists within a List 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