Member-only story

Simple but Not Simple : BitSet in Java

Rakesh singhania
4 min readSep 6, 2024

--

Lets start with a simple program.

Photo by Markus Spiske on Unsplash

You have to write a program to display numbers which are not in array suppose array size is 10.

and given Array a={3,6,8,9};

For this given problem, One solution would be something like this.

import java.util.Arrays;
public class MissingNumbers {
public static void main(String[] args) {
// Given array
int[] a = {3, 6, 8, 9};

// Sort the array (in case it's not already sorted)
Arrays.sort(a);

System.out.println("Given array: " + Arrays.toString(a));
System.out.print("Numbers not in the array: ");

int j = 0; // Index for array 'a'
for (int i = 1; i <= 10; i++) {
if (j < a.length && i == a[j]) {
j++; // Move to next element in array 'a'
} else {
System.out.print(i + " ");
}
}
}
}

But this is not that much efficient , what if i ask you to improve its performance . Here comes bitset for rescue.

What is Bitset and how bitset works ?

BitSet is a useful class in Java for efficiently storing and manipulating a set of bits.

  1. Each bit is represented by a single bit in these long…

--

--

Rakesh singhania
Rakesh singhania

Written by Rakesh singhania

As a student of technology, each day I take a single step forward on the path of learning.

No responses yet