Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. // Find the element that appear once in a sorted array.
  2. public class AppearOnceAlt{
  3. public static void main(String[] args){
  4. int[] arr = new int[]{1,1,1,1,2,2,3,4,4,4,4,5};
  5. appear(arr);
  6. }
  7.  
  8. public static void appear(int[] arr){
  9. int elem = arr[0];
  10. int old = elem;
  11. int count = 0;
  12. for(int i = 1;i<arr.length;i++){
  13. elem = arr[i];
  14. if(elem == old){ count ++;}
  15. else{
  16. if(count == 0 || i == (arr.length -1)){ System.out.println(">" +((count == 0)?old:elem)); }
  17. count = 0;
  18. }
  19. old = elem;
  20. }
  21. }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement