码迷,mamicode.com
首页 > 其他好文 > 详细

961. N-Repeated Element in Size 2N Array

时间:2020-06-20 13:55:05      阅读:38      评论:0      收藏:0      [点我收藏+]

标签:bsp   problem   element   tput   http   key   pre   def   efault   

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

标签:bsp   problem   element   tput   http   key   pre   def   efault   

原文地址:https://www.cnblogs.com/johnnyzhao/p/13168180.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!