static long[] sortAndDeduplicate( long[] labels ) { if ( ArrayUtils.isNotEmpty( labels ) ) { sort( labels ); return PrimitiveLongCollections.deduplicate( labels ); } return labels; }
@Test void shouldDeduplicate() { // GIVEN long[] array = new long[] {1L, 1L, 2L, 5L, 6L, 6L}; // WHEN long[] deduped = PrimitiveLongCollections.deduplicate( array ); // THEN assertArrayEquals( new long[] {1L, 2L, 5L, 6L}, deduped ); }
@Test void shouldDeduplicateWithRandomArrays() { int arrayLength = 5000; int iterations = 10; for ( int i = 0; i < iterations; i++ ) { long[] array = ThreadLocalRandom.current().longs( arrayLength, 0, arrayLength ).sorted().toArray(); long[] dedupedActual = PrimitiveLongCollections.deduplicate( array ); TreeSet<Long> set = new TreeSet<>(); for ( long value : array ) { set.add( value ); } long[] dedupedExpected = new long[set.size()]; Iterator<Long> itr = set.iterator(); for ( int j = 0; j < dedupedExpected.length; j++ ) { assertTrue( itr.hasNext() ); dedupedExpected[j] = itr.next(); } assertArrayEquals( dedupedExpected, dedupedActual ); } }