I have to make a function that receives a row of numbers and returns the number which repeats twice. The row must always fit those rules:
- numbers must be separated by comma row contains only two identical
- numbers, other numbers are unique.
Row examples: "1,2,3,4,5,6,7,7", "10,10,12,15", "1,1,7,8,15" etc.
Here is my code:
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)
{
string q1 = "6,8,7,3,2,5,6";
Console.WriteLine(my_funct(q1));
}
static int my_funct(string q)
{
string[] numbers = q.Split(',');
List<string> numbers2 = new List<string>();
int border = numbers.Count();
for (int i = 0; i < border; i++)
{
if (numbers2.Contains(numbers[i]))
{
return Convert.ToInt32(numbers[i]);
}
else numbers2.Add(numbers[i]);
}
return -1;
}
}
}
I want this function to execute as fast as possible to get the maximal performance? What should I change in my code, what other methods should I use to manage that?