publicclassSolution {
publicintmostFrequentElement(int[] nums) {
int n = nums.length;
int maxCount = 0;
int mostFrequent = nums[0];
// Brute force approach: Compare each element with every other elementfor (int i = 0; i < n; i++) {
int currentCount = 0;
for (int j = 0; j < n; j++) {
if (nums[i]== nums[j]) {
currentCount++;
}
}
// Update the most frequent element if the current count is greaterif (currentCount > maxCount) {
maxCount = currentCount;
mostFrequent = nums[i];
}
}
return mostFrequent;
}
publicstaticvoidmain(String[] args) {
Solution sol =new Solution();
int[] nums = {1, 2, 2, 2, 3, 3};
System.out.println("Most frequent element: "+ sol.mostFrequentElement(nums)); // Expected output: 2 }
}
classSolution:
defmost_frequent_element(self, nums: List[int]) -> int:
n = len(nums)
max_count =0 most_frequent = nums[0]
# Brute force approach: Compare each element with every other elementfor i in range(n):
current_count =0for j in range(n):
if nums[i] == nums[j]:
current_count +=1# Update the most frequent element if the current count is greaterif current_count > max_count:
max_count = current_count
most_frequent = nums[i]
return most_frequent
# Example usagesol = Solution()
nums = [1, 2, 2, 2, 3, 3]
print("Most frequent element:", sol.most_frequent_element(nums)) # Expected output: 2
Let’s understand by example:
Consider the array nums = [2, 3, 3, 5, 3, 4, 1, 7], where n = 8 (number of elements in nums).
Iterate through the input array nums, and for every element nums[i], increment nums[nums[i] % n] by n. The array transforms to [2, 11, 11, 29, 11, 12, 1, 15].
Find the maximum value in the modified array. The index of this maximum value is the most frequent element (the index of 29 is 3).
To restore the original array, iterate through the array again and set nums[i] = nums[i] % n for all i from 0 to n-1.
Note:
If we replace the condition nums[i] / n > max with nums[i] > n, the solution will only print one repeating element and will not work for printing all maximum repeating elements. For example, given [2, 3, 2, 3], the solution would only print 3. To print both 2 and 3 (since both occur the maximum number of times), use the maximum quotient nums[i] / n instead of the maximum value in step 2.
Be cautious as this method may cause overflow if adding n repeatedly makes the value exceed INT_MAX.
publicclassSolution {
publicintmostFrequentElement(int[] arr) {
int n = arr.length;
int maxCount = 0;
int mostFrequent =-1;
// Increment the counts based on modulo operationfor (int i = 0; i < n; i++) {
arr[arr[i]% n]+= n;
}
// Find the maximum value's index (most frequent element)for (int i = 0; i < n; i++) {
if (arr[i]/ n > maxCount) {
maxCount = arr[i]/ n;
mostFrequent = i;
}
}
// Optionally, restore the original arrayfor (int i = 0; i < n; i++) {
arr[i]= arr[i]% n;
}
return mostFrequent;
}
}
classSolution:
defmostFrequentElement(self, arr: List[int]) -> int:
n = len(arr)
max_count =0 most_frequent =-1# Increment the counts based on modulo operationfor i in range(n):
arr[arr[i] % n] += n
# Find the maximum value's index (most frequent element)for i in range(n):
if arr[i] // n > max_count:
max_count = arr[i] // n
most_frequent = i
# Optionally, restore the original arrayfor i in range(n):
arr[i] = arr[i] % n
return most_frequent