How to use to make sorted array elements unique with minimum increments in C#

1 Answer

0 votes
using System;
 
class Program
{
    public static void make_unique(int[] arr) {
        int previous = arr[0];
         
        for (int i = 1; i < arr.Length; i++) {
            if (arr[i] <= previous) {
                arr[i] = previous;
                arr[i]++;
            }
            previous = arr[i];
        }
    }
    static void Main() {
        int[] arr = { 1, 1, 2, 2, 3, 3, 7, 8, 8, 8, 12, 15, 33, 33, 33 };
         
        make_unique(arr);
 
        Console.Write(string.Join(", ", arr));
    }
}
 
 
 
 
 
/*
run:
 
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 33, 34, 35
 
*/

 



answered Dec 11, 2021 by avibootz
edited Dec 11, 2021 by avibootz
...