Reducing sequences in an array of strings

0 votes

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 by Bharani
• 4,550 points
40 views

1 answer to this question.

0 votes

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.

answered Nov 2, 2018 by DataKing99
• 8,100 points

Related Questions In Others

0 votes
1 answer

How can you create an Array in JavaScript?

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

answered Mar 6 in Others by Frankie
• 9,810 points
9 views
0 votes
1 answer

How to store an array in localstorage?

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

answered Jul 1 in Others by sunshine
• 1,200 points
24 views
0 votes
1 answer

How to get the URL of the current tab in Google Chrome?

There are different ways of doing this:- You ...READ MORE

answered Dec 21, 2018 in Others by Nabarupa
60 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Is there a .NET equivalent to Apache Hadoop?

Microsoft dropped its alternative (Dryad) in favor of Hadoop. ...READ MORE

answered Sep 18, 2018 in Big Data Hadoop by Frankie
• 9,810 points
37 views
0 votes
1 answer

Validate String against USPS State Abbreviations

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

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,190 points
20 views
0 votes
1 answer

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

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

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,190 points
30 views
0 votes
1 answer

How to check if array is multidimensional or not?

Since the 'second dimension' could be just ...READ MORE

answered Nov 5, 2018 in Others by DataKing99
• 8,100 points
122 views
0 votes
1 answer