-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleNumber.java
More file actions
25 lines (21 loc) · 892 Bytes
/
Copy pathSingleNumber.java
File metadata and controls
25 lines (21 loc) · 892 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class SingleNumber {
public static void main(String[] args) {
int[] arr = {1, 5, 9, 1, 9};
int result = 0;
// Sum up all elements in the array
for (int i = 0; i < arr.length; i++) {
result += arr[i];
}
// Iterate again to find paired numbers and subtract twice their value
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
// If arr[i] is equal to arr[j] and they are different elements (i != j)
if (arr[i] == arr[j] && i != j) {
result = result -arr[i] * 2; // Subtract the paired number twice
break; // Once we find a pair, move to the next element
}
}
}
System.out.println("The single number is: " + result);
}
}