How to find the element that appears once in array where other elements appear in pairs with Java

1 Answer

0 votes
class FindElementThatAppearsOnceInArray_Java {
     public static int findElementThatAppearsOnceInArray(int[] arr) {
        int result = 0;
        
        for (int i = 0; i < arr.length; i++) {
            result ^= arr[i];
        }
        
        return result;
    }
    public static void main(String[] args) {
        int arr[] = new int[]{3, 5, 2, 7, 3, 2, 8, 8, 5};
        
        System.out.println(findElementThatAppearsOnceInArray(arr));
    }
}



/*
run:

7

*/

 



answered Dec 2, 2021 by avibootz
edited Jul 7, 2024 by avibootz
...