import java.util.ArrayList;
public class Solution {
public static void main(String[] args) {
int []arr = {2,2,1,3,1,1,3,1,1};
List<Integer> result = majorityElement(arr);
System.out.print(result);
}
public static List<Integer> majorityElement(int[] nums) {
int ele1 =-1;
int ele2 =-1;
int count1=0;
int count2=0;
int n= nums.length;
int res = (int)((n/3) + 1);
List<Integer> result = new ArrayList<>(2);
for(int i=0;i<n; i++) {
if(count1==0 && nums[i] != ele2) {
ele1 = nums[i];
count1++;
}
else if(count2==0 && nums[i] != ele1) {
ele2 = nums[i];
count2++;
}
else if(nums[i] == ele1) {
count1++;
if(count1 == res) {
result.add(nums[i]);
}
}
else if(nums[i] == ele2) {
count2++;
if(count2 == res) {
result.add(nums[i]);
}
}
else {
count1--;
count2--;
}
// if(result.size()>2) {
// break;
// }
}
return result;
}
}
#majority element n/3
1 messages · Page 1 of 1 (latest)
Detected code, here are some useful tools:
Formatted code
import java.util.List;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) {
int [] arr = {
2, 2, 1, 3, 1, 1, 3, 1, 1};
List<Integer> result = majorityElement(arr);
System.out.print(result);
}
public static List<Integer> majorityElement(int [] nums) {
int ele1 = - 1;
int ele2 = - 1;
int count1 = 0;
int count2 = 0;
int n = nums.length;
int res = (int ) ((n / 3) + 1);
List<Integer> result = new ArrayList<>(2);
for (int i = 0; i < n; i++) {
if (count1 == 0 && nums[i] != ele2) {
ele1 = nums[i] ;
count1++;
}
else if (count2 == 0 && nums[i] != ele1) {
ele2 = nums[i] ;
count2++;
}
else if (nums[i] == ele1) {
count1++;
if (count1 == res) {
result.add(nums[i] );
}
}
else if (nums[i] == ele2) {
count2++;
if (count2 == res) {
result.add(nums[i] );
}
}
else {
count1--;
count2--;
}
// if(result.size()>2) {
// break;
// }
}
return result;
}
}
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
can someone pls tell why is my output empty
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
nteger> majorityElement(int[] nums) {
List<Integer> result = new ArrayList<>();
int n = nums.length;
int count1 = 0, count2 = 0;
int candidate1 = 0, candidate2 = 1;
for (int num : nums) {
if (num == candidate1) {
count1++;
} else if (num == candidate2) {
count2++;
} else if (count1 == 0) {
candidate1 = num;
count1 = 1;
} else if (count2 == 0) {
candidate2 = num;
count2 = 1;
} else {
count1--;
count2--;
}
}
// Reset counts
count1 = 0;
count2 = 0;
// Count occurrences of candidates
for (int num : nums) {
if (num == candidate1) {
count1++;
} else if (num == candidate2) {
count2++;
}
}
// Check if candidates are majority elements
if (count1 > n / 3) {
result.add(candidate1);
}
if (count2 > n / 3) {
result.add(candidate2);
}
return result;
}
}```