codewars023: FInd the odd int

Instructions:java

Given an array, find the int that appears an odd number of times.app

There will always be only one integer taht appears an odd number of times.code

//--https://www.codewars.com/kata/54da5a58ea159efa38000836/train/java
package codewars;
import java.util.*;
public class FindOdd{
  public static int findIt(int[] A){
    Map<Integer,Integer> map = new HashMap<Integer,Integer>();
    for(int x : A){
      if(map.containsKey(x)){
        map.put(x, map.get(x) + 1);
      }else{
        map.put(x, 1);
      }
    }
    for(Map.Entry<Integer,Integer> x : map.entrySet()){
      if(x.getValue() % 2 == 1){
        return x.getKey();
      }
    }
    return 0;
  }
}
相關文章
相關標籤/搜索