All of my near hits have been while under ATC control, while on an IFR flight plan.
The closest one was about 18 years ago, going into KDAL. I was an FO in a 737-200. We were between Mesquite and KDAL at 300' (IIRC), being vectored to downwind to the 13s. We got a freq change, so I looked down at the radio to make it, As I did, we got a TCAS RA, and before I could even look up, the Capt. said S**T!, disconnected the autopilot, and pulled hard up and left. I looked out my side window, right into the cockpit of an opposite direction 182, right at our altitude. It was so close that I could see that there were 3 guys in flannel shirts, wearing DC headsets, one with a red ball cap. The backseat guy had his head in between the 2 front seats. I don't think they ever even saw us. ATC never said a word about it. Had to clean my shorts after that one!