Majority Element

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.
I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.
Given an array nums of size n, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
LeetCode Problem - 169
import java.util.*;
class Solution {
public int majorityElement(int[] nums) {
// Create an ArrayList to store unique elements from the array.
List<Integer> al = new ArrayList<>();
// Initialize variables to store the majority element and its count.
int ans = 0;
int maxCount = 0;
// Iterate through each element in the array.
for (int num : nums) {
// If the element is not already in the ArrayList, add it.
if (!(al.contains(num))) {
al.add(num);
int count = 0;
// Count the frequency of the current element in the array.
for (int i : nums) {
if (num == i) {
count++;
}
}
// Update the majority element and its count if necessary.
if (count > maxCount) {
maxCount = count;
ans = num;
}
}
}
// Return the majority element.
return ans;
}
}




