Posts

Find Missing Numbers in Array - Java

A method to find all missing numbers in an array within an inclusive range.     int [] findMissingNumbers ( int [] arr , int from , int to ) {          int [] count = new int [ to - from + 1 ] ;           int numEmpty = count . length , k = 0 ; for ( int num : arr )      if ( num >= from && num <= to ) if ( count [ num - from ] ++ == 0 )      numEmpty --; int [] missingNums = new int [ numEmpty ] ; for ( int i = 0 ; i < count . length ; i ++ )      if ( count [ i ] == 0 )           missingNums [ k ++ ] = i + from ; return missingNums ;      } I was trying to use this method as a small step in a random generation for a game I've been working on, and I noticed that most of the code I saw online uses several loops and requires many preconditions, such as the search range not starting or ending within the range of the values of the array, or notably, requiring the array to be presorted. The former c