-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNo_occur_odd_number_of_times_Array.java
More file actions
86 lines (72 loc) · 2.41 KB
/
No_occur_odd_number_of_times_Array.java
File metadata and controls
86 lines (72 loc) · 2.41 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.io.*;
import java.util.*;
public class Main
{
/*
XOR operation of a number with itself results in 0.
Example:
Consider 5 XOR 5
Binary representation for 5 is 101. Hence applying bitwise XOR on 5 following rules given in the above truth table -
101 XOR 101 = 000 = 0
Now, if XOR is applied on the above result with 5, we get 5 XOR 5 XOR 5 as -
000 XOR 101 = 101 = 5
XOR operation on a number with itself even number of times will result in 0.
XOR operation on a number with itself odd number of times will result in the number itself.
*/
public static void printNumberOccuringOddNumberOfTimesXOR(int[] a)
{
if(a == null || a.length == 0)
{
System.out.println("Empty input array.");
return;
}
int r = a[0];
for (int i = 1; i < a.length; i++) {
r = r ^ a[i];
}
System.out.println("Number occurring odd number of times is using XOR operation " + r);
}
public static void printNumberOccuringOddNumberOfTimesHash(int[] a)
{
if(a == null || a.length == 0)
{
System.out.println("Empty input array.");
return;
}
int i;
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(i=0;i<a.length;i++)
{
int key = a[i];
if(map.containsKey(key))
{
int value = map.get(key);
map.put(key, value + 1);
}
else
{
map.put(key,1);
}
}
for (Map.Entry mapElement : map.entrySet())
{
int value = (int)(mapElement.getValue());
if(value%2!=0)
{
System.out.println("Number occurring odd number of times is using HashMAp = " + mapElement.getKey());
break;
}
}
}
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int i,n;
n = Integer.parseInt(br.readLine());
int a[] = new int[n];
for(i=0;i<n;i++)
a[i] = Integer.parseInt(br.readLine());
printNumberOccuringOddNumberOfTimesXOR(a);
printNumberOccuringOddNumberOfTimesHash(a);
}
}