首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 961. N-Repeated Element in Size 2N Array

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

作者头像
Angel_Kitty
发布2019-01-30 16:50:50
5130
发布2019-01-30 16:50:50
举报

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

Example 2:

Input: [2,1,2,5,3,2]
Output: 2

Example 3:

Input: [5,1,5,2,5,3,5,4]
Output: 5

Note:

  1. 4 <= A.length <= 10000
  2. 0 <= A[i] < 10000
  3. A.length is even

题目描述:求一个长度为 2N 数组中重复 N 次的元素值

题目分析:很简单,把每一个出现过的不同元素进行统计,重复次数大于等于 2 的元素即为我们所求。

python 代码:

class Solution(object):
    def repeatedNTimes(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        A_length = len(A)
        for i in range(A_length):
            if A.count(A[i]) >= 2:
                return A[i]
            else:
                continue

C++ 代码:

class Solution {
public:
    int repeatedNTimes(vector<int>& A) {
        for(int i = 0; i < A.size(); i++){
            if(count(A.begin(),A.end(),A[i]) >= 2){
                return A[i];
            }
        }
    }
};
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-01-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档