```
public int wiggleMaxLength(int[] nums) {
int n = nums.length; if(n<=1) return n; //edge condition check
int i=1;
while(i<n&&nums[i]==nums[i-1])//remove the begining duplicate
i++;
if(i>=n-1) return 1;//if all deuplicate
int count = 2;
boolean b = (nums[i-1]>nums[i]);
for(;i<n-1;i++){
if((b&&nums[i]<nums[i+1])||(!b&&nums[i]>nums[i+1])){
b=!b;
count++;
}
}
return count;
}
```