Autonomous vehicles are revolutionizing the future of intelligent transportation systems by integrating smart and intelligent onboard units (OBUs) that minimize human intervention. These vehicles can communicate with their environment and one another, sharing critical information such as emergency alerts or media content. However, this communication infrastructure is susceptible to cyber-attacks, necessitating robust mechanisms for detection and defense. Among these, the most critical threat is the denial-of-service (DoS) attack, which can target any entity within the system that communicates with autonomous vehicles, including roadside units (RSUs), or other autonomous vehicles. Such attacks can lead to devastating consequences, including the disruption or complete cessation of service provision by the infrastructure or the autonomous vehicle itself. In this paper, we propose a system capable of detecting DoS attacks in autonomous vehicles across two scenarios: an infrastructure-based scenario and an infrastructureless scenario, corresponding to vehicle-to-everything communication (V2X) Mode 3 and Mode 4, respectively. For Mode 3, we propose an ensemble learning (EL) approach, while for the Mode 4 environment, we introduce a gossip learning (GL)-based approach. The gossip and ensemble learning approaches demonstrate remarkable achievements in detecting DoS attacks on the UNSW-NB15 dataset, with efficiencies of 98.82% and 99.16%, respectively. Moreover, these methods exhibit superior performance compared to existing schemes.