public static int[] intersectTwoSortedArrays(int[] array1, int[] array2){
		// create a new array having the smallest size between the two arrays
	    final int newArraySize = (array1.length < array2.length) ? array1.length : array2.length;
	    int[] newArray = new int[newArraySize];

	    int pos1 = 0;
	    int pos2 = 0;
	    int posNewArray = 0;
	    while(pos1 < array1.length && pos2 < array2.length) {
	    	if(array1[pos1] < array2[pos2]) {
	    		pos1++;
	    	}else if(array2[pos2] < array1[pos1]) {
	    		pos2++;
	    	}else { // if they are the same
	    		newArray[posNewArray] = array1[pos1];
	    		posNewArray++;
	    		pos1++;
	    		pos2++;
	    	}
	    }
	    // return the subrange of the new array that is full.
	    return Arrays.copyOfRange(newArray, 0, posNewArray); 
	}