To store the number of points has same x or y coordinates
HashMap < Integer , Integer > xpoints = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > ypoints = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( xpoints . containsKey ( a [ i ] [ 0 ] ) ) { xpoints . put ( a [ i ] [ 0 ] , xpoints . get ( a [ i ] [ 0 ] ) + 1 ) ; } else { xpoints . put ( a [ i ] [ 0 ] , 1 ) ; } if ( ypoints . containsKey ( a [ i ] [ 1 ] ) ) { ypoints . put ( a [ i ] [ 1 ] , ypoints . get ( a [ i ] [ 1 ] ) + 1 ) ; } else { ypoints . put ( a [ i ] [ 1 ] , 1 ) ; } }