Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Find the element that appear once in a sorted array.
- public class AppearOnceAlt{
- public static void main(String[] args){
- int[] arr = new int[]{1,1,1,1,2,2,3,4,4,4,4,5};
- appear(arr);
- }
- public static void appear(int[] arr){
- int elem = arr[0];
- int old = elem;
- int count = 0;
- for(int i = 1;i<arr.length;i++){
- elem = arr[i];
- if(elem == old){ count ++;}
- else{
- if(count == 0 || i == (arr.length -1)){ System.out.println(">" +((count == 0)?old:elem)); }
- count = 0;
- }
- old = elem;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement