3

I am writing an algorithm for university. And I have almost everything except the last thing. I now have jagged array of numbers, example of this array:

[0][1]
[1][11,12]
[2][3,7,11,15]
[3][6,7,10,11]

And i need to remove duplicates, like delete every number from all next rows, that is present in the previos row. Output should be something like this:

[0][1]
[1][11,12]
[2][3,7,15]
[3][6,10]

I have tried something like this:

for (int i = 0; i <= numbers.Length + 1; i++)
{
    int size = numbers[i].Length;
    for (int j = 0; j < size; j++)
    {
        if (numbers[i][numbers[i].Length] != numbers[i + 1][numbers[i + 1].Length])
        {
            newNumbers[i][j] = numbers[i][j];
        }
    }
}

But it does not work the way it should.

6
  • Is [0] [1] [2] [3] part of array, or just indexes? Commented Jan 12, 2020 at 9:39
  • 1
    Why are you comparing lengths in your if statement ? Commented Jan 12, 2020 at 9:49
  • It's the if statement and the contents within if statement that is causing issue. Make sure you make correct use of i and j variable to manipulate array. Commented Jan 12, 2020 at 9:52
  • You may need to have a secondary data structure (such as a dictionary) which stores the duplicate number and it’s locations. Your first pass of the array populates this dictionary- the second pass uses this information to remove duplicates Commented Jan 12, 2020 at 9:56
  • Pavel Anikhouski they are indexes. Commented Jan 12, 2020 at 9:57

2 Answers 2

3

You can solve it by using Except method from System.Linq namespace.

At every loop iteration go through all next rows and get the difference between these rows and current one and reassign it back. It's possible, because jagged array is an array whose elements are arrays and you have the same int data type for all items

int[][] jaggedArray = new int[4][];

jaggedArray[0] = new[] { 1 };
jaggedArray[1] = new[] { 11, 12 };
jaggedArray[2] = new[] { 3, 7, 11, 15 };
jaggedArray[3] = new[] { 6, 7, 10, 11 };

for (int i = 0; i < jaggedArray.Length; i++)
{
    var currentRow = jaggedArray[i];
    for (int j = i + 1; j < jaggedArray.Length; j++)
    {
        var result = jaggedArray[j].Except(currentRow);
        jaggedArray[j] = result.ToArray();
    }
}

If you print the result array

foreach (var array in jaggedArray)
{
    foreach (var item in array) 
        Console.Write($"{item} ");

    Console.WriteLine();
}

The output will be the following

result

Sign up to request clarification or add additional context in comments.

3 Comments

Would that work if there’s duplicate numbers in non adjacent rows?
OP mentioned that delete every number from all next rows, that is present in the previos row. The internal for loop go through all next rows until end of array, at every iteration of main loop
@PavelAnikhouski it's working, thank you very much. You just saved me from sitting next few hours and trying to achive this.
1
public static int[][] RemoveDuplicates(this int[][] data)
{
    var result = new int[data.Length][];
    var used = new List<int>();

    for (int i = 0; i < data.Length; i++)
    {
        var hashSet = new HashSet<int>(data[i].Except(used));
        result[i] = hashSet.ToArray();
        used.AddRange(hashSet);
    }
    return result;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.