D
D
Dmitry2022-01-30 14:32:48
C++ / C#
Dmitry, 2022-01-30 14:32:48

Is it possible to remove duplicate elements from an array with less code than me?

Is there a way to improve (using more advanced c# features) my algorithm?

int[] array = { 1, 2, 33, 44, 44,44,44 ,33, 22, 15 };
for (int i = 0; i < array.Length; i++)
{
    for (int k = i+1; k < array.Length; k++)
    {
        if (array[i] == array[k])
        {
            array[i] = 0;
        }
    }
}
int[] numbers = new int[array.Length - array.Count(n => n == 0)];
int j = 0;
for (int i = 0; i < array.Length; i++)
{
    if(array[i] != 0)
    {
        numbers[j] = array[i];
        j++;
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2022-01-30
@roflanPominki

Look at Distinct()

M
Myclass, 2022-01-30
@Myclass

The redesigned Counting Sort came up I think better.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question