raxbg

Longest sequence

Mar 20th, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.53 KB | None | 0 0
  1.     public static void main(String[] args) {
  2.         int arr[] = {1,2,3,4,5,6,6,6,7,2,2};
  3.         int max[] = new int[2];
  4.         int tmp[] = new int[2];
  5.         max[0] = tmp[0] = arr[0];
  6.         max[1] = tmp[1] = 1;
  7.         for (int i=1;i<arr.length;i++){
  8.             if(arr[i] == tmp[0]) tmp[1]++;
  9.             else{
  10.                 if(tmp[1] >= max[1]){
  11.                 max[0] = tmp[0];
  12.                 max[1] = tmp[1];
  13.                 }
  14.                 tmp[0] = arr[i];
  15.                 tmp[1] = 1;
  16.             }
  17.         }
  18.         if(tmp[1] >= max[1]){
  19.             max[0] = tmp[0];
  20.             max[1] = tmp[1];
  21.         }
  22.         System.out.println("Max " +max[1]+ " equal elements of "+max[0]+"s");
  23.     }
Advertisement
Add Comment
Please, Sign In to add comment