Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8387053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:06:43+00:00 2026-06-09T18:06:43+00:00

I’m learning C# and trying to solve the following problem: Return the longest subarray

  • 0

I’m learning C# and trying to solve the following problem:
Return the longest subarray of repeating members e.g. if the array is {1,2,2,3,4,4,4} I should return {4,4,4}. I tried to do this but it returns the first subarray and not the longest. What I know about C# so far:

  • Loops
  • Conditionals
  • Arrays

Any ideas ?

EDIT : My code so far
EDIT : Yes I do know something about multidemensional arrays

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sequence
{
class Sequence
{
    static void Main(string[] args)
    { 
        Console.Write("Enter size:");
        int size1 = int.Parse(Console.ReadLine());
        int[] array1 = new int[size1];
        for (int i = 0; i <= size1-1; i++)
        {
            Console.Write ("Ënter Number:");
            array1[i]=Int32.Parse(Console.ReadLine());
        }
        int bestLenght = 0;
        int bestStart = 0;
        int lenght = 0;
        int start=0;
        for (int i = 0; i < size1 - 2; i++)
        {
            if (i == 0 && array1[i] == array1[i + 1])
            {
                start = 0;
                lenght = 2;
                if (bestLenght < lenght)
                {
                    bestLenght = lenght;
                    bestStart = 0;
                }
            }
            else if (i != 0 && lenght != 0 && array1[i] == array1[i - 1] && array1[i + 1]   ==              array1[i])
            {
                lenght++;
                if (bestLenght < lenght)
                {
                    bestLenght = lenght;
                    bestStart = start;
                }
            }
            else if (i != 0 && array1[i - 1] != array1[i] && array1[i] == array1[i + 1])
            {
                start = i;
                lenght = 2;
                if (bestLenght < lenght)
                {
                    bestLenght = lenght;
                    bestStart = start;
                }

            }
            else 
            {
                lenght = 0;
            }


        }
        Console.WriteLine(bestLenght);



    }
}

}

Cureently I’m trying just to return the lenght of the longest array

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:06:45+00:00Added an answer on June 9, 2026 at 6:06 pm

    EDIT: The problem with your code is it doesn’t deal with the edge case when the longest list is the last sublist

    Change

    Console.WriteLine(bestLenght); 
    

    To Read

    if (lenght > bestLenght) {
      bestLenght=lenght;
      bestStart=start;
    }
    Console.WriteLine(bestLenght); 
    

    Alternatively

    You can do this with a linq Agregate

    var x= new[] {1,2,2,3,4,4,4};
    var y=x.Aggregate(Tuple.Create(new List<int>(),new List<int>()),
    (a,b) =>{
       if (a.Item2.Count()>0 && a.Item2[0] != b) {
         if (a.Item2.Count>a.Item1.Count()) {
           a=Tuple.Create(a.Item2,new List<int>());
         }
         a.Item2.Clear();
       }
       a.Item2.Add(b);
       return a;
    },a=>(a.Item2.Count() > a.Item1.Count()) ? a.Item2 : a.Item1);
    

    What this essentially does is iterate over the collection using a Tuple to store 2 lists, Item1 represents the longest previous sequence, Item2 represents the current sequence.

    For each item, if the current sequence is not empty and the first item is different then we are in a new sublist so check the length of last sequence, if longer that previous max we replace previous max otherwise just clear the list.

    The final part of the agregate checks which of the two lists are longer (as if the longests subcollection is the last the Item1 length check won’t have happened.

    This code could be turned into a generic function to handle any type as follows.

    IEnumerable<T> LongestSublist<T>(IEnumerable<T> source) {
    return source.Aggregate(Tuple.Create(new List<T>(),new List<T>()),
        (a,b) =>{
           if (a.Item2.Count()>0 && a.Item2[0] != b) {
             if (a.Item2.Count>a.Item1.Count()) {
               a=Tuple.Create(a.Item2,new List<T>());
             }
             a.Item2.Clear();
           }
           a.Item2.Add(b);
           return a;
        },a=>(a.Item2.Count() > a.Item1.Count()) ? a.Item2 : a.Item1);
    

    or even as an extension function

    public static IEnumerable<T> LongestSublist<T>(this IEnumerable<T> source) {
    return source.Aggregate(Tuple.Create(new List<T>(),new List<T>()),
        (a,b) =>{
           if (a.Item2.Count()>0 && a.Item2[0] != b) {
             if (a.Item2.Count>a.Item1.Count()) {
               a=Tuple.Create(a.Item2,new List<T>());
             }
             a.Item2.Clear();
           }
           a.Item2.Add(b);
           return a;
        },a=>(a.Item2.Count() > a.Item1.Count()) ? a.Item2 : a.Item1);
    

    allowing you to do

    var longest= new[] {1,2,2,3,4,4,4}.LongestSubList();

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.