findDuplicates

Sorts array and iterates over each array element that compares equal to the previous element.

To just check for the existence of duplicates it's recommended to make found() return true (or some other value different from 0) to stop the iteration after the first duplicate.

To assert array has no duplicates or throw an exception if it has, put the assert(false) or throw ... in found():

int[] array;

findDuplicates(array,
               (ref size_t index, ref int element)
               {
                   throw new Exception("array contains duplicates");
                   return 0; // pacify the compiler
               });

Template params: T = type of array element sort = true: do array.sort first; false: array is already sorted

int
findDuplicates
(
T
bool sort = true
)
(
T[] array
,
scope int delegate
(
ref size_t index
,
ref T element
)
found
)

Parameters

array T[]

array to clean from duplicate values

found int delegate
(
ref size_t index
,
ref T element
)

foreach/opApply() style delegate, called with the index and the value of each array element that is equal to the previous element, returns 0 to continue or a value different from 0 to stop iteration.

Return Value

Type: int

- 0 if no duplicates were found so found() was not called or - 0 if found() returned 0 on each call or - the non-zero value returned by found() on the last call.

Meta