Advertisement
Shekhar777

Java Loops and Introduction to Arrays - Find unique

Oct 19th, 2020
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.57 KB | None | 0 0
  1. Q15-
  2. Given an array of N elements. In the array, each element is present twice except for 1 element whose frequency in the array is 1. Hence the length of the array will always be odd.
  3. Find the unique number.
  4. Ans-import java.io.*;
  5. import java.util.*;
  6. class Main {
  7.     public static void main (String[] args) {
  8.         Scanner inputScanner = new Scanner(System.in);
  9.         int n = inputScanner.nextInt();
  10.         int A[] = new int[n];
  11.         int xorResult = 0;
  12.         for(int i=0;i<n;i++){
  13.             A[i] = inputScanner.nextInt();
  14.             xorResult = xorResult^ A[i];
  15.         }
  16.         System.out.print(xorResult);
  17. }
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement