Alternate patch for 1518, using counting method

Signed-off-by: A V Minhaz <minhazav@gmail.com>