Skip to content

Instantly share code, notes, and snippets.

@unilecs
Created July 4, 2019 04:24
Show Gist options
  • Save unilecs/a647883763a8eeb4af181ea0c9da42c9 to your computer and use it in GitHub Desktop.
Save unilecs/a647883763a8eeb4af181ea0c9da42c9 to your computer and use it in GitHub Desktop.
Задача: Distinct Array
using System;
using System.Linq;
public class Program
{
private static int DistinctArray(ref int[] arr)
{
int i, j;
for (i = 0, j = 1; j < arr.Length; j++)
{
if (arr[i] != arr[j])
{
i++;
arr[i] = arr[j];
}
}
return i + 1; // size of squashed array (w/o duplicated)
}
public static void Main()
{
Console.WriteLine("UniLecs");
int[] arr = new int[] { -9, -9, -9, 0, 0, 1, 2, 2, 5, 5, 6 };
int newSize = DistinctArray(ref arr);
Console.WriteLine(string.Format("Sorted arr w/o duplicates = [{0}]", string.Join(", ", arr.Take(newSize))));
arr = new int[] { 1, 1, 1, 3, 3, 5, 7, 7, 7, 7, 11 };
newSize = DistinctArray(ref arr);
Console.WriteLine(string.Format("Sorted arr w/o duplicates = [{0}]", string.Join(", ", arr.Take(newSize))));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment