Write a function:
class Solution { public int solution(int[] A); }
that, given an array A consisting of N integers fulfilling the above conditions, returns the value of the unpaired element.
For example, given array A such that:
A[0] = 9 A[1] = 3 A[2] = 9
A[3] = 3 A[4] = 9 A[5] = 7
A[6] = 9
the function should return 7, as explained in the example above.
用 HashMap 100 % 過了XD
// you can also use imports, for example: // import java.util.*; import java.util.*; // you can write to stdout for debugging purposes, e.g. // System.out.println("this is a debug message"); class Solution { public int solution(int[] A) { // write your code in Java SE 8 Map<Integer,Integer> g = new HashMap<Integer, Integer>(); for(int i=0;i< A.length;i++ ){ if ( g.get(A[i]) == null ) { g.put(A[i],1); }else{ g.put(A[i],g.get(A[i])+1); } } Integer keyArr[] = g.keySet().toArray(new Integer[g.size()]); int ind = 0; for ( int i=0;i<keyArr.length;i++){ if ( g.get(keyArr[i]) % 2 != 0 ){ ind = keyArr[i]; } } return(ind); } }
用 HashMap 100 % 過了XD
// you can also use imports, for example: // import java.util.*; import java.util.*; // you can write to stdout for debugging purposes, e.g. // System.out.println("this is a debug message"); class Solution { public int solution(int[] A) { // write your code in Java SE 8 Map<Integer,Integer> g = new HashMap<Integer, Integer>(); for(int i=0;i< A.length;i++ ){ if ( g.get(A[i]) == null ) { g.put(A[i],1); }else{ g.put(A[i],g.get(A[i])+1); } } Integer keyArr[] = g.keySet().toArray(new Integer[g.size()]); int ind = 0; for ( int i=0;i<keyArr.length;i++){ if ( g.get(keyArr[i]) % 2 != 0 ){ ind = keyArr[i]; } } return(ind); } }