How to use Stack in c# with struct?

  c++

i need to rewrite this code using C#,can you help me , plz
I have code in c++ , but dont know how to realise this algorithm in c#.

#include <iostream>
#include <stack>

int main()
{
int n;
std::cin >> n;

struct N { int v, c; };
std::stack<N> s;
int cnt{};

while (n--) {
    int i;
    std::cin >> i;

    l:
    if (s.empty()) s.push({ i, 1 });
    else if (i == s.top().v) ++s.top().c;
    else if (s.top().c > 2) {
        cnt += s.top().c;
        s.pop();
        goto l;
    }
    else s.push({ i, 1 });
}
if (s.top().c > 2) cnt += s.top().c;
std::cout << cnt;

return 0;
}

Source: Windows Questions C++

LEAVE A COMMENT