Use Set instead of Array to keep track of ids to be copied.