U
U
Uncle Bogdan2020-11-06 13:07:45
C++ / C#
Uncle Bogdan, 2020-11-06 13:07:45

Why does the method throw an error?

It is necessary that the ArrayDiff method remove all the same elements from a, that is, if a = 1,2,2,1 and b = 1, then the result = 2,2
Error:
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Kata.ArrayDiff (System.Int32[] a, System.Int32[] b) (at Assets/Test.cs:16)
Test.Start () (at Assets/Test.cs:27)
Code:

using System.Collections.Generic;
using UnityEngine;

public static class Kata
{

    public static int[] ArrayDiff(int[] a, int[] b)
    {
        List<int> result = new List<int>();
        for(int BNumber = 0; BNumber != b.Length; BNumber++)
        {
            for(int ANumber = 0; ANumber != a.Length; ANumber++)
            {
                if (a[ANumber] != b[BNumber])
                {
                    result.RemoveAt(ANumber);
                }
            }
        }
        return result.ToArray();
    }
}
public class Test : MonoBehaviour
{
    void Start()
    {
        int[] array = Kata.ArrayDiff(new int[] { 2, 2, 1, 2 }, new int[] { 2 });
        foreach(int i in array)
        {
            Debug.Log( array[i] );
        }
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question