Refined code by skipping duplicates rather than check expected