The fastest way to find if an int array contains a number

This is a strange question. I have an integer array in Java where each int represents a color. They will be either 0xFFFFFFFF or 0x0. What would be the fastest way to find if this array contains ANY values ​​equal to 0xFFFFFFFF?

This is my current code:

int length = w * h;
for (int i = 0; i < length; i++) {
    if (pixels[i] == 0xFFFFFFFF) {
        return true;
    }
}

I have no clue if there is a faster way to do this or not. I suggest that you vets might get a trick or two though.

EDIT: Seeing that this is just a dumb array of pixels from Bitmap.getPixels (), it will not sort or convert to another storage structure. Thanks for the contribution, everyone seems to have scrolling the best way in this case.

+5
9

, , , , .

"O (n)". , , , , .

+11

- , , . , , , - , .

, . , :

  • , , , , . , , . , , , , . , O (1), , .

  • , - (, ), . , , , , , , , , - , "" .

  • , ( ), , . , , .

  • , , - , , , (, ), , . , , . , , - - , .

  • , - , -, O (1) , . , .

  • , - - , . (, , ) .

  • , , , , - . , , , . . , , , (, O (log n)) . , .

  • , , bitvector. (, 32-128x). , - 0, , - . , , .

, !

+11

-,

if (pixels[i] != 0)

, , , , .

+2

, , . , (, t , t - ). parallelism .

+1

, : . (templatetypedef .) 25% :

tmp = a[n - 1]
a[n - 1] = 0xFFFFFFFF

pos = 0
while a[pos] != 0xFFFFFFFF
    pos = pos + 1

a[n - 1] = tmp

if a[pos] = 0xFFFFFFFF then
    return pos
return -1

# .

+1

- . , , .

int length = w * h;
for (int i = 0; i < length; i++) {
    if (pixels[i] & 0xFFFFFFFF) {
        return true;
    }
}
0

, ? , , 0xFFFFFFFF. , "" , : D

, O (n), , ( , ) .

0

foreach, , , id

for(int pix:pixels){
    if(pix!=0)
        return true;
}
-1
source
Arrays.asList(...).contains(...)
-1
source

All Articles