Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array Slice to create new array

Tags:

arrays

c#

How can I assign a slice of an array to a new array. I am trying to assign the maximal consecutive of equal elements to a new array.

I think I have the basic logic right but cannot slice to a new array. This is exercise 4 in Arrays - Fundamentals of computer programming with c#.

using System;
using System.Collections.Generic;

class ArrayEx4
{
    static void Main()
    {
        int[] testArray = { 1, 1, 2, 3, 2, 2, 2, 1 }; // Output wanted {2,2,2};

        int count = 0;
        int bestCount = 0;
        int startArray = 0;
        int endArray = 0;
        int first = 0;
        for (int i = 0; i < testArray.Length; i++)
        {
            if (testArray[i] == testArray[i+1])
            {
                count += 1;
                i = first;
                if (count > bestCount)
                {
                    count = bestCount;
                    startArray = first;
                    endArray = first + bestCount;
                }
            }
            int[] bestArray = testArray.Slice(startArray, endArray);
            Console.WriteLine("The best array is  {0}", bestArray);
        }

    }
}

I am getting a directive error so I added using System.Collections.Generic; but I am still getting this error.

C:\Users\Sayth\Documents\Scripts>csc ArrayEx4.cs
Microsoft (R) Visual C# Compiler version 4.0.30319.33440
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.

ArrayEx4.cs(28,32): error CS1061: 'System.Array' does not contain a definition
        for 'Slice' and no extension method 'Slice' accepting a first argument
        of type 'System.Array' could be found (are you missing a using directive
        or an assembly reference?)

EDIT

The logic in my solution above never worked. Here is the updated code including answer below as working.

using System;
using System.Linq;

class Arr4
{
    static void Main()
    {
        int[] testArray = { 1, 1, 2, 3, 2, 2, 2, 1 }; // Output wanted {2,2,2};
        int count = 0;
        int bestCount = 0;
        int start = 0;
        int a = 0;

        for(int i = 0; i < testArray.Length -1; i++)
        {
            while (testArray[i] == testArray[a])
            {
                count += 1;
                a += 1;
                if (count > bestCount)
                {
                    start = i;
                    bestCount = count;
                }
            }
        }

     int end = bestCount + start;
     Console.WriteLine("The start is {0} and count is {1}", start, bestCount);
     var bestArray = testArray.Skip(start)
                      .Take(end - start)
                      .ToArray();
        Console.Write("{ ");
        Console.Write(string.Join(", ", bestArray));
        Console.Write(" }");
    }   

}
like image 406
sayth Avatar asked Dec 26 '22 06:12

sayth


1 Answers

Using Linq you could do this:

var bestArray = testArray.Skip(startArray) 
                         .Take(endArray - startArray)
                         .ToArray();

(Don't forget to add using System.Linq)

like image 122
BrokenGlass Avatar answered Jan 03 '23 10:01

BrokenGlass