Assuming you are not a US resident for 2017, the First Year of Residency rules will apply, which for someone who is resident by passing the Substantial Presence Test, will make you a resident only for the part of the year after you first enter the US, and nonresident for the part of the year before that. So that already makes you a dual-status alien for 2018; you can choose to be treated as a resident for the whole years only if you are married.
As for the last part of the year, there also does exist Last Year of Residency rules, but unlike for First Year of Residency, there are additional conditions. You can qualify to have a residency termination date be the last day you are in the US instead of the end of the year, but only if for the rest of the year, your tax home was in a foreign country, and you had a "closer connection to a foreign country".