w3resource
C# Sharp Programming Exercises

C# Sharp Basic Algorithm Exercises: Check if one of the first 4 elements in an array of integers is equal to a given element


C# Sharp Basic Algorithm: Exercise-33 with Solution

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.

Pictorial Presentation:

C# Sharp: Basic Algorithm Exercises - Check if one of the first 4 elements in an array of integers is equal to a given element

Sample Solution:-

C# Sharp Code:

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);
        }
   }
}

Sample Output:

True
True
False

Flowchart:

C# Sharp: Flowchart: Check if one of the first 4 elements in an array of integers is equal to a given element

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to count a substring of length 2 appears in a given string and also as the last 2 characters of the string.Do not count the end substring.
Next: Write a C# Sharp program to check whether the sequence of numbers 1, 2, 3 appears in a given array of integers somewhere

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming