java - Get equal values within a stack -


i'm doing stack on java contains 5 integers have print out values equal.

example 1 - 2 - 2 - 3 - 4 same number is: 2

how can determine make same numbers?

here code:

package e.d_pilas;  import java.util.*;  public class ed_pilas {     private int stck[];     private int tos;      ed_pilas(int size){         //new stack         stck = new int[size];         tos = -1;     }      void push(int value) {         stck[++tos] = value;     }      int pop() {         if (tos < 0) {             return 0;         } else             return stck[tos--];     }      public static void main(string[] args) {         int number;         scanner read = new scanner (system.in);         system.out.print("enter 5 (5) numbers fill stack \n");         ed_pilas stack = new ed_pilas(5);          (int = 1; < 6; i++){             system.out.print("enter value "+i+" fill stack \n");             number=read.nextint();             stack.push(number);          }         system.out.println("equal values contained in stack: \n");          (int j = 1; j < 6; j++){             system.out.println("\t " + stack.pop());             }      } } 

thank you!

in first method print duplicates entries 1 time. in second method if stack contains more 2 entries print multiple times.

        arraylist<int> list=new arraylist<int>();          (int j = 1; j < 6; j++){             int num=stack.pop();             if(list.contains(num)){                system.out.println(num);             }             else{                  list.add(num);             }          }  

or stack.pop() methods removes element use stack.search() method find duplicates

            (int j = 1; j < 6; j++){              int num=stack.pop();              if(stack.search(num)==1){                system.out.println(num);             }             else{              }          }  

Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -