

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# program to check if two lists have at-least one element common
Set the first list.
int[] arr1 = { 65, 57, 63, 98 };
Now, set the second list.
int[] arr2 = { 43, 65, 33, 57 };
Let us now see the complete code to check if two lists have common elements using == and < operators.
Example
using System; using System.Collections.Generic; using System.Linq; public class Program { public static void Main() { int[] arr1 = { 65, 57, 63, 98 }; int[] arr2 = { 43, 65, 33, 57 }; // HashSet One var h1 = new HashSet < int > (arr1); // HashSet Two var h2 = new HashSet < int > (arr2); // Displaying int[] val1 = h1.ToArray(); Console.WriteLine("Set one..."); foreach(int val in val1) { Console.WriteLine(val); } //Displaying int[] val2 = h2.ToArray(); Console.WriteLine("Set two..."); foreach(int val in val2) { Console.WriteLine(val); } int i = 0, j = 0; Console.WriteLine("Common elements:"); while (i < val1.Length && j < val2.Length) { if (val1[i] == val2[j]) { Console.Write(val1[i] + " "); i++; j++; } else if (val1[i] < val2[j]) i++; else j++; } } }
Output
Set one... 65 57 63 98 Set two... 43 65 33 57 Common elements: 65 57
- Related Questions & Answers
- Python program to check if two lists have at least one common element
- Python - Check if two lists have any element in common
- How to aggregate two lists if at least one element matches in MongoDB?
- Check if both halves of the string have at least one different character in Python
- Checking if decimals share at least two common 1 bits in JavaScript
- Count divisors of n that have at-least one digit common with n in Java
- Program to check every sublist in a list containing at least one unique element in Python
- How to check if a string has at least one letter and one number in Python?
- C++ program to count how many minutes we have to wait to meet at least one swimmer
- Program to check whether every one has at least a friend or not in Python
- Check if two lists are identical in Python
- C# program to find common values from two or more Lists
- C# program to print all the common elements of two lists
- Python program to print all the common elements of two lists.
- Common element with least index sum in JavaScript
Advertisements