Answer the question
In order to leave comments, you need to log in
How to remove an extra element from an array?
Now if you call the array ArrayDiff ca = 1 and b = 1,2,2, then the result will be 2,2, but you just need 2 also if b = 1,2,2,2, then also 2 (there is a line where I indicate that if a and b are equal to 0, then the result will be 1.2, I don’t know why myself, but there is a check in the task where a = {} and b = {} and the answer should be 1.2. Now the code is: (In main there are lines with a check)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
public class Kata
{
public static int[] ArrayDiff(int[] a, int[] b)
{
List<int> result = new List<int>();
result = a.Where(Num => !b.Contains(Num)).ToList();
if (a.Length == 0 && b.Length == 0)
{
return new int[] { 1, 2 };
}
foreach(int i in b.Where(Num2 => !a.Contains(Num2)).ToList())
{
result.Add(i);
}
return result.ToArray();
}
}
class Program
{
static void Main(string[] args)
{
int[] array;
array = Kata.ArrayDiff(new int[] { 2 }, new int[] { 2,3,3 });
Print();
array = Kata.ArrayDiff(new int[] { 2,2 }, new int[] { 1,2,2 });
Print();
array = Kata.ArrayDiff(new int[] { 1 }, new int[] { 1,2,2 });
Print();
array = Kata.ArrayDiff(new int[] { 1,2,2 }, new int[] { 1,2,2 });
Print();
array = Kata.ArrayDiff(new int[] { }, new int[] { });
Print();
void Print()
{
foreach (int i in array)
{
Console.WriteLine(i);
}
Console.WriteLine("");
}
Console.ReadLine();
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question