# A C# solution. Comments welcome.

• ``````public class Solution {
public int WiggleMaxLength(int[] nums) {
int length = nums.Length;
if (length < 3) return length;
int last = nums[1];
int dir = nums[0] < nums[1] ? 1 : -1;
int wiggle = 2;
for(int i = 2; i < length; i++)
{
int next = nums[i];
if ((last - next) * dir > 0)
{
wiggle++;
last = next;
dir *= -1;
}
else
{
last = next;
}
}
return wiggle;
}
}
``````

• Fails input `[0,0]`.

• Yup. You are right. I was assuming that consecutive numbers would be different. Will fix it.

• Here it is. This one handles duplicates.

``````public class Solution {
public int WiggleMaxLength(int[] nums) {
int length = nums.Length;
if(length < 2) return length;
int d = 1;
while(d < length && nums[d] == nums[0])
{
d++;
}

if(d >= length) return 1;

int last = nums[d];
int dir = nums[0] < last ? 1 : -1;
int wiggle = 2;
for(int i = d; i < length; i++)
{
int next = nums[i];
if(next == last)
{
continue;
}

if ((last - next) * dir > 0)
{
wiggle++;
last = next;
dir *= -1;
}
else
{
last = next;
}
}
return wiggle;
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.