I'm looking for the best way to remove sequences from an array.

Let's say the following is a sequence of elements in an array:

```[0] a
[1] a
[2] b
[3] c
[4] c
[5] a
[6] c
[7] d
[8] c
[9] d```

And, the following is what I want to obtain:

```[0] a
[1] b
[2] c
[3] a
[4] c
[5] d```

So, irrespective of duplicate elements, sequences of the same element get reduced to just a single instance of that element. And, whenever two lines repeat they also get reduced to one set like this:

```[0] c
[1] d
[2] c
[3] d```

gets reduced to:

```[0] c
[1] d```

I've been trying to write the code for it in C# but working solutions in any other language would also be a great help.

Nov 2, 2018 in Others 814 views

## 1 answer to this question.

I've written a C# app to solves reduce sequences and it works like a charm.

Input: aabccacdcd

Output: abcacd

```class Program
{
private static List<string> values;
private const int MAX_PATTERN_LENGTH = 4;

static void Main(string[] args)
{
values = new List<string>();
values.AddRange(new string[] { "a", "b", "c", "c", "a", "c", "d", "c", "d" });

for (int i = MAX_PATTERN_LENGTH; i > 0; i--)
{
RemoveDuplicatesOfLength(i);
}

foreach (string s in values)
{
Console.WriteLine(s);
}
}

private static void RemoveDuplicatesOfLength(int dupeLength)
{
for (int i = 0; i < values.Count; i++)
{
if (i + dupeLength > values.Count)
break;

if (i + dupeLength + dupeLength > values.Count)
break;

var patternA = values.GetRange(i, dupeLength);
var patternB = values.GetRange(i + dupeLength, dupeLength);

bool isPattern = ComparePatterns(patternA, patternB);

if (isPattern)
{
values.RemoveRange(i, dupeLength);
}
}
}

private static bool ComparePatterns(List<string> pattern, List<string> candidate)
{
for (int i = 0; i < pattern.Count; i++)
{
if (pattern[i] != candidate[i])
return false;
}

return true;
}
}```

The dynamic pattern length bit surely gave me a hard time but you can see I've changed the initial values to match the ones in your question.

• 8,240 points

## How can you create an Array in JavaScript?

You can define arrays using the array literal as ...READ MORE

## How to store an array in localstorage?

Localstorage only supports Strings. So you can ...READ MORE

+1 vote

## Between cyber security and CCNA profession which one is best in terms of time to become an expert and salary payment

CCNA professional is more inclined towards the ...READ MORE

## count() parameter must be an array or an object that implements countable in laravel

You will have to make one change ...READ MORE

## SQLite.Net not being able to create text file in Win IoT Library!

That exception comes when access limited to ...READ MORE

## Validate String against USPS State Abbreviations

Try something like this: private static String states ...READ MORE

## Authenticate on an ASP.Net Forms Authorization website from a console app

Essentially, we need to record a regular ...READ MORE