961. N-Repeated Element in Size 2N Array

Posted johnnyzhao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了961. N-Repeated Element in Size 2N Array相关的知识,希望对你有一定的参考价值。

package LeetCode_961

/**
 * 961. N-Repeated Element in Size 2N Array
 * https://leetcode.com/problems/n-repeated-element-in-size-2n-array/description/
 *
In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times.
Return the element repeated N times.

Example 1:
Input: [1,2,3,3]
Output: 3
 * */
class Solution {
    fun repeatedNTimes(A: IntArray): Int {
        /*val n = A.size/2
        val map = HashMap<Int,Int>()
        for (i in A){
            map.put(i, map.getOrDefault(i,0)+1)
        }
        for (m in map){
            if (m.value==n){
                return m.key
            }
        }*/

        //because exactly one of these elements is repeated N times
        val array = IntArray(10000)
        for (num in A){
            array[num]++
            if (array[num]>1){
                return num
            }
        }
        return 0
    }
}

 

以上是关于961. N-Repeated Element in Size 2N Array的主要内容,如果未能解决你的问题,请参考以下文章

961. N-Repeated Element in Size 2N Array

LeetCode 961. N-Repeated Element in Size 2N Array

Leetcode 961. N-Repeated Element in Size 2N Array

Leetcode 961. N-Repeated Element in Size 2N Array

[Leetcode]961. N-Repeated Element in Size 2N Array

leetcode961. N-Repeated Element in Size 2N Array