You are viewing a single comment's thread. Return to all comments →
Java Solution: Easy way is to use XOR operation: sum ^= a[i]; By doing so every paired number will become 0 and sum will remain the unique number.
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { private static int lonelyInteger(int[] a) { return 0; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; int sum = 0; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); sum ^= a[i]; } //System.out.println(lonelyInteger(a)); System.out.println(sum); } }
Seems like cookies are disabled on this browser, please enable them to open this website
Lonely Integer
You are viewing a single comment's thread. Return to all comments →
Java Solution: Easy way is to use XOR operation: sum ^= a[i]; By doing so every paired number will become 0 and sum will remain the unique number.