-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleOccurrenceNumbers.java
More file actions
33 lines (28 loc) · 1.04 KB
/
Copy pathSingleOccurrenceNumbers.java
File metadata and controls
33 lines (28 loc) · 1.04 KB
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
26
27
28
29
30
31
32
33
import java.util.*;
public class SingleOccurrenceNumbers{
public static void main(String[] args) {
// Define an array of integers
int nums[] = {10, 20, 10, 20, 30, 40, 40, 30, 50};
// Declare a variable to store the result
int result;
// Display the source array
System.out.println("Source Array : " + Arrays.toString(nums));
// Calculate and display the number that appears only once
result = getSingleNumber(nums);
System.out.println("\n" + result + " appears only once.");
}
public static int getSingleNumber(int[] nums) {
// Check if the array is null or empty
if (nums == null || nums.length == 0) {
return -1;
}
// Initialize the result to 0
int result = 0;
// Calculate the number that appears only once using XOR (^) operator
for (int i = 0; i < nums.length; i++) {
result ^= nums[i];
}
// Return the result
return result;
}
}