I'm trying to understand the implementation of below statement found on stackoverflow
Can use BitSet, as you walk the array of values, set the corresponding bit to true. Then you can walk over the bit set and output the corresponding value whenever you find a bit set to true
I know very simple way of removing duplicates from array of int. But struggling to understand how it can be implemented using above statement found on this site.
ref link
https://stackoverflow.com/questions/3667543/remove-duplicates-from-a-large-integer-array-using-java
can anyone suggest how it can be implemented ?
CodePudding user response:
You can do it like so:
BitSet bs = new BitSet();
// walk the array of values
for (int i : array) {
// set the corresponding bit to true
bs.set(i);
}
// walk over the bit set
for (int i = 0; i < bs.size(); i) {
// output the corresponding value whenever you find a bit set to true
if (bs.get(i)) {
System.out.println(i);
}
}
As noted by ewramner, BitSet is not sparse, so this will allocate enough bits for everything up to the largest value in the array. If there are very few values in the array, or they are all a lot larger than zero, this will probably be quite inefficient, and putting things in a Set (e.g. a HashSet or TreeSet) would be a better option.
This is a more efficient way to walk over the bitset looking for true values:
for (int i = -1; (i = bs.nextSetBit(i 1)) != -1;) {
System.out.println(i);
}
