i need to check if there is duplicates in stack (use only in the basic method pop,push,top,isempty) and return true if the stack have not duplicate or return true if the stack is empty and return false if the stack have duplicates
public static bool CheckStack(Stack<int> s, int x)
{
Stack<int> s1 = new Stack<int>();
bool flag = false;
if (s.IsEmpty())
return false;
while (!s.IsEmpty() && ! flag)
{
int temp = s.Pop();
s1.Push(temp);
if (temp == x)
flag = true;
}
s = s1;
return flag;
}
public static bool SpecialStack(Stack<int> s)
{
int temp = s.Pop();
while(!s.IsEmpty() && !CheckStack(s,temp))
{
temp = s.Pop();
}
if (s.IsEmpty())
return true;
return false;
}
public static void Main()
{
Stack<int> st3 = new Stack<int>();
st3.Push(7);
st3.Push(2);
st3.Push(1);
st3.Push(8);
st3.Push(7);
Console.WriteLine(SpecialStack(st3));
}
how can i do that without use another function and the shortest way the solution needs to be with basic method without use build in function of c#