-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPairElements.java
More file actions
40 lines (32 loc) · 867 Bytes
/
Copy pathPairElements.java
File metadata and controls
40 lines (32 loc) · 867 Bytes
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
//Print out the unpaired element from the array.
package Test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class PairElements {
public static void Solution(int[] arr){
HashMap<Integer, Integer> hp = new HashMap<Integer, Integer>();
for(int i=0;i<arr.length;i++){
int count = 0;
for(int j=0;j<arr.length;j++){
if(arr[i] == arr[j]){
count++;
hp.put(arr[i], count);
}
}
}
Iterator it = hp.entrySet().iterator();
while(it.hasNext()){
Map.Entry<Integer,Integer> pair = (Map.Entry) it.next();
//System.out.println(pair.getKey()+ " = " +pair.getValue());
if(pair.getValue() %2 != 0){
System.out.println(pair.getKey());
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = {9,3,9,3,9,7,9};
Solution(arr);
}
}