Ad
Code
Diff
  • //Given 2 Arrays, Return True if arrays contain common item, else false.
    //e.g a= ['a','b','g','c'] and b =['z','e','c'] returns true
    //input : 2 arrays
    //output: bool
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public class Kata{
      public static bool ContainsCommonItem(char[] a, char[] b){
            // linear complexity on arrays length
            bool isNullOrEmpty(char[] arr) => arr == null || arr.Length == 0;
            if (isNullOrEmpty(a) || isNullOrEmpty(b))
            {
                return false;
            }
            // works only for ascii characters
            // if you need to handle non-ascii characters you should use bigger buffer
            var chars = new short[256];
            // mark all characters that exist in 'a' array
            foreach (var t in a)
            {
                chars[Convert.ToByte(t)] = 1;
            }
    
            return b.Any(t => chars[Convert.ToByte(t)] > 0);
      }
    }
    
    • //Given 2 Arrays, Return True if arrays contain common item, else false.
    • //e.g a= ['a','b','g','c'] and b =['z','e','c'] returns true
    • //input : 2 arrays
    • //output: bool
    • using System;
    • 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;
    • return a.Intersect(b).Any();
    • }
    • public class Kata{
    • public static bool ContainsCommonItem(char[] a, char[] b){
    • // linear complexity on arrays length
    • bool isNullOrEmpty(char[] arr) => arr == null || arr.Length == 0;
    • if (isNullOrEmpty(a) || isNullOrEmpty(b))
    • {
    • return false;
    • }
    • // works only for ascii characters
    • // if you need to handle non-ascii characters you should use bigger buffer
    • var chars = new short[256];
    • // mark all characters that exist in 'a' array
    • foreach (var t in a)
    • {
    • chars[Convert.ToByte(t)] = 1;
    • }
    • return b.Any(t => chars[Convert.ToByte(t)] > 0);
    • }
    • }