
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
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 Articles
- 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
- Program to check every sublist in a list containing at least one unique element 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
- 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
- C# program to find common values from two or more Lists
- Python program to print all the common elements of two lists.
- C# program to print all the common elements of two lists
- Check if two lists are identical in Python
- Python program to check whether two lists are circularly identical

Advertisements