title: How to remove duplicates from stack?
body: <p><strong>TASK:</strong> Let's say there is an integer stack S with M elements. Give the algorithm that will remove all those numbers from stack S that appear two or more times. (write the task using C/C++)</p>
  
  <p><strong>NOTE:</strong> We are not allowed to use <code>std::stack</code> to solve this task.</p>
  
  <p>First of all I decided to use C language, and this is stack implementation I use.</p>
  
  <pre><code>int* stack = (int*)malloc(10 * sizeof(int));
  int size = 10;
  int sp = -1;
  
  bool isempty() {
      return (sp == -1);
  }
  
  bool isfull() {
      return (sp == size - 1);
  }
  
  void push(int x) {
      if (isfull()) {
          printf("Full!");
      }
      else {
          sp++;
          stack[sp] = x;
      }
  }
  
  int pop() {
      int x;
      if (isempty()) {
          printf("Empty!");
      }
      else {
          x = stack[sp];
          sp--;
      }
      return x;
  }
  
  void peek() {
      if (!isempty()) {
          printf("%d", stack[sp]);
      }
  }
  
  void clear() {
      while (!isempty()) {
          pop();
      }
  }
  
  void print() {
      if (!isempty()) {
          for (int i = 0; i &lt; sp+1; i++) {
              printf("%d ", stack[i]);
          }
      }
      printf("\n");
  }
  </code></pre>
  
  <p>My idea of solving this task was to make another <em>temp</em> stack and copy main <em>stack</em> into it, than use two <em>for</em> loops to compare all elements and inside that I used <em>if</em> statment to check if they are same or not, if they are not same I just pushed them into back into stack that was previously cleared, by this way I'm supposed to skip all duplicate elements but for some reason this code is not working properly it keeps <em>spamming</em> me "Full!" message.</p>
  
  <pre><code>void removeDuplicates() {
      int* temp = (int*)malloc(10 * sizeof(int));
      int temp_size = 10;
      int temp_sp = -1;
  
      for (int i = 0; i &lt; sp + 1; i++) {
          temp[i] = stack[i];
      }
      temp_sp = sp;
      clear();
  
      for (int i = 0; i &lt; temp_sp+1; i++) {
          for (int j = i + 1; j &lt; temp_sp+1; i++) {
              if (!(temp[i] == temp[j])) {
                  push(temp[i]);
              }
          }
      }
  }
  </code></pre>
  
  <p>This is main function that I used to test out functions:</p>
  
  <pre><code>int main() {
      push(1);
      push(2);
      push(3);
      push(4);
      push(3);
      push(5);
  
      removeDuplicates();
  
      print();
  
      return 0;
  }
  </code></pre>
  
  <p>If there is simpler way to solve this by using C++ (not <code>std::stack</code>), let me know.</p>