Java: Find the smallest integer not in the list - Memory Efficiency This is a fairly popular interview question that I conceptually understand but have never really attempted to implement. In other similar implementations of this in Java I have seen they typically operate on an array of boolean values which kind of irks me.  It turns out that the way the JVM works however is that it will allocate 8 bits for a single boolean primitive.  Granted I believe that an array of booleans actually does allocate a single bit for each value but just as a personal challenge I decided to use an array of byte and through bitwise operations utilizing each bit for a boolean value of N.

This means that the byte array will really be N / 8 or a single bit representing each possible random number that COULD occur in a list of numbers that just happens to be of N length as well.

The strategy is to construct my bytes by scanning each randomly generated number, determining which byte and bit that number correlates to in my byte array, and flipping the appropriate bit when that number is encountered.  When the scan is finished I simply inspect each bit and the first 0 bit I encounter means that bit location in my array represents the smallest integer that is not in the list of randomly generated numbers.

It should consume roughly 1/8th of memory from if I were to use a List and is 1/4 faster than sorting the list of random numbers.

import junit.framework.TestCase;

public class TestSmallestNumberNotInList extends TestCase {

    static final int N = 30000000;
    static int[] numbers = null;
    static {
        numbers = new int[N];
        for (int i = 0; i &lt; numbers.length; i++)
            numbers[i] = Math.abs((int)(Math.round(Math.random() * N)));
    }

    public void testFindSmallestNumberNotInList() throws Exception {

        long startTime = System.currentTimeMillis();

        int byteArrayLength = (int) Math.ceil(N / 8);
        byte[] bitset = new byte[byteArrayLength];

        for (int number : numbers) {
            int byteIndex = (int)Math.floor(number / 8);
            byte b = bitset[byteIndex];
            int bitMod = (int)(number % 8);

            // The bit value will be 1 if we encountered this number before
            int bitVal = b&gt;&gt;&gt;(32-bitMod) &amp; 0x00000001;
            if (bitVal == 1)
                continue;

            //Get a byte mask to OR against the existing byte, flipping just the one bit
            //we are concerned with.
            byte byteMask = (byte)(1&lt;&lt;bitMod);
            b = (byte)(b | byteMask);

            //assign the new byte value for the byte array
            bitset[byteIndex] = b;
        }

        //Scan the byte array for the first 0 bit, this ((index * 8) + bitmodulus) is the smallest
        //integer not in the list.
        int location = 0;
        for (int i = 0; i &lt; bitset.length; i++) {
            byte b = bitset[i];
            if (b == 0xFF) {
                location += 8;
                continue;
            } else {
                for (int j=8; j&gt;=1; j--) {
                    if ((b&gt;&gt;&gt;(j) &amp; 0x00000001) == 1)
                        location++;
                }
                break;
            }
        }

        long duration = System.currentTimeMillis() - startTime;

        System.out.println("Total Memory: " + Runtime.getRuntime().totalMemory());
        System.out.println("Free Memory: " + Runtime.getRuntime().freeMemory());
        System.out.println("Smallest number not in the list is " + location);
        System.out.println("Time to execute in milliseconds: " + duration);
    }

    private static String getBitString(int x) {
        StringBuffer buf = new StringBuffer();
        for (int i=1; i&lt;=32; i++) buf.append(x&gt;&gt;&gt;(32-i) &amp; 0x00000001);
        return buf.toString();
    }
}


I believe the output is correct although I really haven't unit tested the results.  It certainly seems correct.  I was wondering if I am making any glaring mistakes in my arithmetic or code or if you have any other possibilities or suggestions for improvement?
So just a few things that I am seeing here.  You are using Math.floor, ceil on integers which is totally unnecessary, integers by definition don't have a decimal portion.  You also don't need to cast an int as an int.  

To make this run faster and take up less stack space, in the for loop where you set the bits you don't need to check anything, just do the bit twiddling you want.

int bitMod = 0x1 &lt;&lt; (number % 8);
byte b = number / 8;
bitset[b] |= bitMod;


Getting the position of the bit you want to change and then just or ing it won't change any other bits.

Finally, I don't know why you did it the way you did, but your final for loop doesn't work correctly.  It starts with the Most Significant Bit and moves down, instead of the least moving up.  Here is a version that I have tested and found to work.

for (int j=0; j&lt;8; j--) {
    if ((b &amp; (1 &lt;&lt; j)) != 0){
        location++;
    } else {
        break;
    }
}


Finally, just to see what kind of memory this might be using, I called the free memory before and after to compare the difference.  It looks like this program uses 56 MB, and takes under 100 ms after my few changes.

actual lowest is 1
(Initial Implementation)
Free Memory: 563760
Smallest number not in the list is 2
Time to execute in milliseconds: 156
2 is in the list true

(Optimized and cleaned up)
Free Memory: 563760
Smallest number not in the list is 1
Time to execute in milliseconds: 93
1 is in the list false


Here is the class I used to test.

public class TestSmallestNumberNotInList {

static final int N = 3000000;
static int[] numbers = null;
static {
    numbers = new int[N];
    for (int i = 0; i &lt; numbers.length; i++) {
        numbers[i] = Math.abs((int)(Math.round(Math.random() * N)));
    }
}

public void testFindSmallestNumberNotInList() {

    Runtime.getRuntime().gc();
    Runtime.getRuntime().gc();
    Runtime.getRuntime().gc();
    long total = Runtime.getRuntime().totalMemory();
    long free = Runtime.getRuntime().freeMemory();
    long startTime = System.currentTimeMillis();

    int byteArrayLength = (int) Math.ceil(N / 8);
    byte[] bitset = new byte[byteArrayLength];

    for (int number : numbers) {
        int byteIndex = (int)Math.floor(number / 8);
        byte b = bitset[byteIndex];
        int bitMod = (int)(number % 8);

        // The bit value will be 1 if we encountered this number before
        int bitVal = b&gt;&gt;&gt;(32-bitMod) &amp; 0x00000001;
        if (bitVal == 1)
            continue;

        //Get a byte mask to OR against the existing byte, flipping just the one bit
        //we are concerned with.
        byte byteMask = (byte)(1&lt;&lt;bitMod);
        b = (byte)(b | byteMask);

        //assign the new byte value for the byte array
        bitset[byteIndex] = b;
    }

    //Scan the byte array for the first 0 bit, this ((index * 8) + bitmodulus) is the smallest
    //integer not in the list.
    int location = 0;
    for (int i = 0; i &lt; bitset.length; i++) {
        byte b = bitset[i];
        if (b == 0xFF) {
            location += 8;
            continue;
        } else {
            for (int j=8; j&gt;=1; j--) {
                if ((b&gt;&gt;&gt;(j) &amp; 0x00000001) == 1)
                    location++;
            }
            break;
        }
    }

    long duration = System.currentTimeMillis() - startTime;

    System.out.println("Total Memory: " + (total - Runtime.getRuntime().totalMemory()));
    System.out.println("Free Memory: " + (free - Runtime.getRuntime().freeMemory()));
    System.out.println("Smallest number not in the list is " + location);
    System.out.println("Time to execute in milliseconds: " + duration);
    boolean isInList = false;
    for (int next : numbers){
        if (location == next){
            isInList = true;
            break;
        }
    }
    System.out.println(location + " is in the list " + isInList);
}

public void testMine() {
    Runtime.getRuntime().gc();
    Runtime.getRuntime().gc();
    Runtime.getRuntime().gc();
    long total = Runtime.getRuntime().totalMemory();
    long free = Runtime.getRuntime().freeMemory();
    long startTime = System.currentTimeMillis();

    int byteArrayLength = numbers.length / 8;
    byte[] bitset = new byte[byteArrayLength];

    for (int number : numbers) {
        int byteIndex = number / 8;
        int bitMod = (1 &lt;&lt; (number % 8)) &amp; 0xFF;
        bitset[byteIndex] |= bitMod;
    }

    //Scan the byte array for the first 0 bit, this ((index * 8) + bitmodulus) is the smallest
    //integer not in the list.
    int location = 0;
    for (int i = 0; i &lt; bitset.length; i++) {
        byte b = bitset[i];
        if (b == 0xFF) {
            location += 8;
            continue;
        } else {
            for (int j=0; j&lt;8; j++) {
                if ((b &amp; (1 &lt;&lt; j)) != 0){
                    location++;
                } else {
                    break;
                }
            }
            break;
        }
    }

    long duration = System.currentTimeMillis() - startTime;

    System.out.println("Total Memory: " + (total - Runtime.getRuntime().totalMemory()));
    System.out.println("Free Memory: " + (free - Runtime.getRuntime().freeMemory()));
    System.out.println("Smallest number not in the list is " + location);
    System.out.println("Time to execute in milliseconds: " + duration);

    boolean isInList = false;
    for (int next : numbers){
        if (location == next){
            isInList = true;
            break;
        }
    }
    System.out.println(location + " is in the list " + isInList);
}


public void getSmallestNotInList() {
     int lowest = 0;
     boolean notLowest = true;
     while (notLowest){
         int last = lowest;
         for (int i=0; i&lt;numbers.length; i++){
             if (numbers[i] == lowest){
                 lowest++;
                 break;
             }
         }
         if (last == lowest){
             break;
         }
     }
     System.out.println("actual lowest is " + lowest);
}
}