Detect duplicate values in primitive Java array

前端 未结 9 1200
别跟我提以往
别跟我提以往 2020-12-18 08:21

I want to detect duplicate values in a Java array. For example:

int[] array = { 3, 3, 3, 1, 5, 8, 11, 4, 5 };

How could I get the specific

9条回答
  •  半阙折子戏
    2020-12-18 09:14

    Sort the array, then either scan it or Arrays.binarySearch + scan in either direction. Due to much fewer allocations and no wrapping, this can be faster, especially on larger arrays.

提交回复
热议问题