-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #183 from Soumita072/patch-1
Create gfg_potd_18Oct_2024.java
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import java.util.*; | ||
|
||
class Solution { | ||
public int getSingle(int[] arr) { | ||
// Using XOR to find the number occurring an odd number of times | ||
int result = 0; | ||
for (int num : arr) { | ||
result ^= num; | ||
} | ||
return result; | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
int t = Integer.parseInt(scanner.nextLine()); | ||
|
||
while (t-- > 0) { | ||
String input = scanner.nextLine(); | ||
String[] parts = input.split(" "); | ||
int[] arr = new int[parts.length]; | ||
|
||
for (int i = 0; i < parts.length; i++) { | ||
arr[i] = Integer.parseInt(parts[i]); | ||
} | ||
|
||
Solution obj = new Solution(); | ||
int res = obj.getSingle(arr); | ||
System.out.println(res); | ||
} | ||
|
||
scanner.close(); | ||
} | ||
} |