Melanoma, despite its relatively low incidence compared to other types of skin cancer, accounts for a significant proportion of skin cancer-related deaths. Early detection of melanoma is crucial for improving patient survival rates. Deep learning algorithms, which heavily rely on data, are widely used in melanoma detection. However, the performance of these algorithms is greatly influenced by the distribution of the dataset, particularly class imbalance. In this manuscript, the authors present a novel method based on the Kemeny–Young rule for optimal rank aggregation to address the class imbalance problem in melanoma detection. The proposed approach aims to reduce class bias and enhance overall classification accuracy. Furthermore, a cost-sensitive learning approach is introduced to improve the classifier’s ability to handle class imbalance effectively. This novel cost-sensitive learning method utilizes Self-Adaptive Differential Evolution Optimization to determine optimal weights for each class. Our approach differs from traditional methods that assign weights based on predefined criteria. To evaluate the effectiveness of the proposed methods, extensive experiments and ablation studies are conducted on the highly imbalanced ISIC 2020 dataset, which is widely used in melanoma detection research. The Kemeny–Young rule-based majority voting achieves an overall error rate of 2.44%, while the cost-sensitive learning based on the Self-Adaptive Differential Evolution approach achieves an even lower error rate of 1.99%. Moreover, the proposed method achieves a sensitivity of 87.93% and a specificity of 98.19%. These experimental results demonstrate the competitiveness and effectiveness of the proposed methods in addressing the challenges posed by class imbalance and improving the accuracy of melanoma detection. By effectively mitigating class imbalance, these methods improve the accuracy and reliability of melanoma detection, thus offering valuable insights for developing advanced computer-aided diagnosis systems in dermatology. The relevant codes for our proposed approach are publicly available at: https://github.com/ctrl-gaurav/Handling-Imbalanced-Class-in-Melanoma.