Q:

Write a C# Sharp program to check if one of the first 4 elements in an array of integers is equal to a given element

0

Write a C# Sharp program to check if one of the first 4 elements in an array of integers is equal to a given element

Sample Output:

True
True
False

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;
using System.Linq;
namespace exercises
{
   class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(test(new[] {1,2,9,3}, 3));
            Console.WriteLine(test(new[] {1,2,3,4,5,6}, 2));
            Console.WriteLine(test(new[] {1,2,2,3}, 9));
            Console.ReadLine();
        }   
       public static bool test(int[] numbers, int n)
        {
            return numbers.Length < 4 ? numbers.Contains(n) : numbers.Take(4).Contains(n);
        }
   }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now