Repository: TheAlgorithms/Java
Pull request #5633 (2024-10-08 04:51:18)

Add `NumberAppearingOddTimes.java` new Algorithm
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

Adds the Algorithm 1 as in #5628 



codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5633?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `80.00000%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 58.16%. Comparing base [(`9fb8192`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/9fb819235643b8a6afa392b65e72a46036c4c210?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`ac31972`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/ac319724c59ed99ba4e63ee39caea018c6a6d385?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5633?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...ithms/bitmanipulation/NumberAppearingOddTimes.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5633?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fbitmanipulation%2FNumberAppearingOddTimes.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9iaXRtYW5pcHVsYXRpb24vTnVtYmVyQXBwZWFyaW5nT2RkVGltZXMuamF2YQ==) | 80.00% | [1 Missing :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5633?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5633      +/-   ##
============================================
- Coverage     58.17%   58.16%   -0.01%     
- Complexity     3687     3688       +1     
============================================
  Files           548      549       +1     
  Lines         15832    15837       +5     
  Branches       3012     3013       +1     
============================================
+ Hits           9210     9212       +2     
- Misses         6230     6232       +2     
- Partials        392      393       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5633?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java b/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java
new file mode 100644
index 00000000..ce4e1d88
--- /dev/null
+++ b/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java
@@ -0,0 +1,21 @@
+package com.thealgorithms.bitmanipulation;
+
+/**
+ * Find the Number Appearing Odd Times in an array
+ * @author Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999)
+ */
+
+public final class NumberAppearingOddTimes {
+    private NumberAppearingOddTimes() {
+    }
+    public static int findOddOccurrence(int[] arr) {
+        int result = 0;
+
+        // XOR all elements in the array
+        for (int num : arr) {
+            result ^= num;
+        }
+
+        return result;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java b/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java
new file mode 100644
index 00000000..d10b0f67
--- /dev/null
+++ b/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java
@@ -0,0 +1,23 @@
+package com.thealgorithms.bitmanipulation;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+class NumberAppearingOddTimesTest {
+
+    @Test
+    void testFindOddOccurrence() {
+        int[] arr1 = {5, 6, 7, 8};
+        assertEquals(12, NumberAppearingOddTimes.findOddOccurrence(arr1));
+
+        int[] arr2 = {2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2};
+        assertEquals(5, NumberAppearingOddTimes.findOddOccurrence(arr2));
+
+        int[] arr3 = {10, 10, 20, 20, 30};
+        assertEquals(30, NumberAppearingOddTimes.findOddOccurrence(arr3));
+
+        int[] arr4 = {-5, -5, -3, -3, -7, -7, -7};
+        assertEquals(-7, NumberAppearingOddTimes.findOddOccurrence(arr4));
+    }
+}