using System.Collections.Generic; using System.Linq; public class Kata { /// <summary> /// Checks if two char arrays contain at least one common item. /// </summary> /// <param name="a">First char array</param> /// <param name="b">Second char array</param> /// <returns>True if the arrays have at least one common item, false otherwise</returns> public static bool ContainsCommonItem(char[] a, char[] b) { // If either input array is null, return false if (a == null || b == null) return false; foreach (char ch in a) { if (b.Contains(ch)) return true; } return false; } }
- using System.Collections.Generic;
- using System.Linq;
- public class Kata
- {
- /// <summary>
- /// Checks if two char arrays contain at least one common item.
- /// </summary>
- /// <param name="a">First char array</param>
- /// <param name="b">Second char array</param>
- /// <returns>True if the arrays have at least one common item, false otherwise</returns>
- public static bool ContainsCommonItem(char[] a, char[] b)
- {
- // If either input array is null, return false
- if (a == null || b == null) return false;
// Create a HashSet from the first array to optimize lookupHashSet<char> setA = new HashSet<char>(a);// Use LINQ's Any method to check if any item in the second array is present in the HashSetreturn b.Any(item => setA.Contains(item));- foreach (char ch in a)
- {
- if (b.Contains(ch)) return true;
- }
- return false;
- }
- }