2019-07-28

codewar卡塔

Given an array, find the int that appears an odd number of times.
There will always be only one integer that appears an odd number of times.
给定一个数组,找到出现奇数次的int。
始终只有一个整数出现奇数次。

public class FindOdd {
    public static int findIt(int[] a) {
    int odd = 0;
    for (int i = 0;i<a.length;i++){
      odd ^= a[i];//0与任何数异或都为该数本身,两个一样的数异或为0
    }
    return odd;
  }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,163评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,482评论 0 23
  • 那片云,飘来飘去 变换千样姿态 万种风情,落在水面 风掠过,水皱,云碎 抬望眼,已不是那片 云的心思,光阴早已窥见...
    浮光_掠影阅读 3,823评论 9 20
  • 前两天晚上,玩手游“绝地求生·刺激战场”。4排,即4个人一组,100个人从飞机上跳下来,找枪找装备,相互战斗厮杀,...
    Fang2023阅读 2,990评论 1 3
  • 简介 Jvm 系列一:Java类的加载机制Jvm系列二:JVM内存结构 --内存泄漏与内存溢出Jvm系列三:GC算...
    上山走18398阅读 2,454评论 0 0