Detect map and material instances!

Map instances for textures:

I guess it should be like this: when detected that it has instances when looping through maps then assign 1, then all other textures instances should be applied with 1 as well. after scanning the next texture with its instances they will be applied with 2, 3 and so on! If the texture doesn't have instances then 0

if isKindOf map BitmapTexture do 
   append newbitmaps map
--???---if map has instances then
           mapinst=1
        else mapinst =0 
myfunction newbitmaps mapinst 

smth like this for materials as well!

I would like also a way of detecting similar proprieties for maps and materials and then to make them instances!

Any help is highly appreciated! Thank you!!!