-
-
Save unilecs/a647883763a8eeb4af181ea0c9da42c9 to your computer and use it in GitHub Desktop.
Задача: Distinct Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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