Find a Number in a List

Given a list of numbers, the function will return the index of the number if it exists in the list. If it does not exist, the function will return -1.


Given a list of numbers, return the index of the number in the list if it exists. If it does not exist, return -1.
Input: [1, 3, 5, 7, 9]
Output: 2


This solution is in C#. Our A.I. can create solutions in multiple languages.
by kayakane
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
    class Program
        static void Main(string[] args)
            int[] arr = { 1, 3, 5, 7, 9 };
            int num = 3;
            int index = -1;
            for (int i = 0; i < arr.Length; i++)
                if (arr[i] == num)
                    index = i;

A.I. Evaluation of the Solution

The candidate's solution correctly solves the problem. However, it is not the most efficient solution. A more efficient solution would be to use a binary search algorithm, which would run in O(log n) time.

Evaluated at: 2022-12-02 06:15:34