0

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#

exd199
  • 1
  • 2
  • Since this is _not_ about a specific programming problem, I think it might be better in [codereview.se] – Soner Gönül May 09 '21 at 12:03
  • Does this answer your question? [C# LINQ find duplicates in List](https://stackoverflow.com/questions/18547354/c-sharp-linq-find-duplicates-in-list) – Trevor May 09 '21 at 13:01

1 Answers1

0

Problem #1: Stack is cleared

Regarding the code itself, at first glance we need :

bool CheckStack (ref Stack<int> s, int x)

To restore the stack, otherwise rethink the design is needed because the original is empty at the end of the method, and so after the return.

Passing Objects By Reference or Value in C#

Passing Reference Types by Reference (C# Programming Guide)

enter image description here

Problem #2: Stack is reverted

Also by pop and push the original stack is reverted in the shallow copy.

Last in, first out

enter image description here

enter image description here

Problem #3: SpecialStack

I have doubts about this method unless the goal is to empty the stack one item at a time and call CheckStack on each iteration until there is no more items.

Simple Linq solution

We can use Linq Distinct and compare each Count :

Console.WriteLine(st3.Distinct().Count() != st3.Count);

Thus if the counts match there is no duplicates, else there are some.

Generic extension method improvement

static public class StackHelper
{
  static public bool HasDuplicates<T>(this Stack<T> stack)
  {
    return stack.Distinct().Count() != stack.Count;
  }
}

Console.WriteLine(st3.HasDuplicates()); // True

Solution without using any other method than Pop, Push, Contains and Count as requested

static public bool HasDuplicates<T>(this Stack<T> stack)
{
  // Check for duplicates
  bool nodup = true;
  var temp = new Stack<T>();
  while ( stack.Count != 0 )
  {
    var item = stack.Pop();
    if ( nodup && temp.Contains(item) ) nodup = false;
    temp.Push(item);
  }
  // Restore original stack
  while ( temp.Count != 0 )
  {
    stack.Push(temp.Pop());
  }
  return !nodup;
}

Note: T can be replaced by int to remove the genericity and the method can be declared as needed, extension or not, static or instance...