Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 353a9a9

Browse files
xor
1 parent 4473d16 commit 353a9a9

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

‎README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
# About
2-
This repository contains Software Engineering concepts.
1+
### Handy refresher guide
32

3+
- Bitwise operators
4+
- [XOR - takes two operands, evaluates to true only when both are different](https://github.com/ankurjuneja/React-Java-Concepts/tree/master/src/XOR.java)
45
- [ReactJS](https://github.com/ankurjuneja/React-Java-Concepts/blob/master/ReactJs/Introduction.md)
56
- Java
67
- [Arrays and Strings](https://github.com/ankurjuneja/React-Java-Concepts/blob/master/ArraysAndStrings/Tutorial.md)

‎src/XOR.java‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
/**
3+
* * XOR - bitwise operator
4+
* * takes two operands, evaluates to true only when both are different
5+
*
6+
* Write a function findMissing that takes two arrays and returns missing element from the second array.
7+
* findMissing(
8+
* [3,1,9,2,6] , [1,2,6,9]
9+
* ) => 3
10+
*
11+
* Assumptions -
12+
* 1. array of non negative unique integers
13+
* 2. exactly one element missing
14+
*
15+
*
16+
* */
17+
public class XOR
18+
{
19+
20+
/**
21+
* Time complexity - O(n) [Linear]
22+
* Space complexity -
23+
* Concept - 1^3^1 = 1
24+
* */
25+
static int findMissing(int[] arrOne, int[] arrTwo)
26+
{
27+
int xor_all = 0;
28+
29+
for (int num : arrOne)
30+
xor_all ^= num;
31+
32+
for (int num : arrTwo)
33+
xor_all ^= num;
34+
35+
return xor_all;
36+
}
37+
38+
public static void main(String[] args)
39+
{
40+
int missingElement = XOR.findMissing(new int[]{3,1,9,2,6}, new int[]{1,2,6,9});
41+
System.out.println("Missing Element is = " + missingElement);
42+
}
43+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /