Task description

A non-empty zero-indexed array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired.

For example, in 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 elements at indexes 0 and 2 have value 9,
  • the elements at indexes 1 and 3 have value 3,
  • the elements at indexes 4 and 6 have value 9,
  • the element at index 5 has value 7 and is unpaired.

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.

Assume that:

  • N is an odd integer within the range [1..1,000,000];
  • each element of array A is an integer within the range [1..1,000,000,000];
  • all but one of the values in A occur an even number of times.

Complexity:

  • expected worst-case time complexity is O(N);
  • expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).

Elements of input arrays can be modified.

Solution

 
Programming language used: Java
Total time used: 4 minutes
Code: 10:06:09 UTC, java, final, score:  100
// you can also use imports, for example:
// 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
int length = A.length;
int number = 0;
for(int i=0; i<length; i++) {
number ^= A[i];
}
return number;
}
}

Codility----OddOccurrencesInArray的更多相关文章

  1. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  2. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  3. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  4. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  5. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

  6. *[codility]AscendingPaths

    https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...

  7. *[codility]MaxDoubleSliceSum

    https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...

  8. *[codility]Fish

    https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...

  9. *[codility]CartesianSequence

    https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...

  10. [codility]CountDiv

    https://codility.com/demo/take-sample-test/count_div 此题比较简单,是在O(1)时间里求区间[A,B]里面能被K整除的数字,那么就计算一下就能得到. ...

随机推荐

  1. 一个2013届毕业生(踏上IT行业)的迷茫(1)

    从毕业到现在已经快半年了,已经想写这篇文字很久了,但是一次又一次的被没有时间给搁置了,今天突然好想写一篇自己这么多年的总结,算是一种反思,也可以看作为未来的人生指路吧. 我和很多搞IT的同行们一样,不 ...

  2. 程序员,用NuGet管理好你的包包(转)

    每个女人都有很多包包:其实男人也有,但只有会写程序的男人才有 —— 代码世界中的大“包”小“包”.这些大包小包,有花钱买的,有从开源市场淘的,也有自己或同事亲手制作的. 包包有个特点:容易坏,更新快, ...

  3. Windows多线程系列

    来自CSDN - 秒杀多线程系列.覆盖了Windows系统的线程同步机制.对于理解各种锁以及多线程典型场景很有帮助.

  4. /var/tmp/.oracle 和 oracle listener (监听)的一点理解

    关于 /var/tmp/.oracle 的作用測试 ~---查看 /var/tmp 的权限 [root@lixora var]# ll total 164 ... drwxrwxrwt  3 root ...

  5. 在vs中启动项目,同时给项目传递参数

    问题的引出:项目在startup.cs文件中做了控制,根据读取的控制台的ip 和端口启动项目 : dotnet project --ip 127.0.0.1 --port 8001 这样写的好处是  ...

  6. matlab 微积分

    符号变量,symbolic variable 1. 高阶导数 高阶导数的计算,当然可以用手工的方式,但显然这种机械重复的推导,更适用于计算机的计算方式: f(x)=sinxx2+4x+3⇒d4fdx4 ...

  7. Matlab Tricks(二十三)—— 保存图像到 pdf

    printme = @(txt) print('-dpdf', sprintf('figures/Example_%s',txt)); % 该匿名函数的接受的参数为字符串类型,也即欲保存的文件名: % ...

  8. matplotlib简单的新手教程和动画

    做数据分析,首先是要熟悉和理解数据,所以掌握一个趁手的可视化工具是很重要的,否则对数据连个主要的感性认识都没有,怎样进行下一步的design 点击打开链接 还有一个非常棒的资料  Matplotlib ...

  9. .NET Framework 源代码

    微软在线源代码:http://referencesource.microsoft.com/ 压缩包:http://download.csdn.net/detail/xunzaosiyecao/8497 ...

  10. Android Ant 和 Gradle 包装工艺和效率控制

    一个.Ant 包:(下载ant.配置环境变量不说) 1.进入命令行模式,并切换到项目文件夹,运行例如以下命令为ADT创建的项目加入ant build支持: android update project ...