How to Find N-Repeated Element in Size 2N Array?

  • 时间:2020-10-12 15:56:23
  • 分类:网络文摘
  • 阅读:165 次

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:
4 <= A.length <= 10000
0 <= A[i] < 10000
A.length is even

Set, HashSet

The element must be the only element that is duplicate in the array, therefore we can use set or hashset (or even hash table) to store the numbers that we have known when iterating the array. As long as it appears before in the set, we output the number.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
    public int repeatedNTimes(int[] A) {
        Set<Integer> set = new HashSet<>();
        for (int a: A) {
            if (set.contains(a)) {
                return a;
            }
            set.add(a);   
        }
        throw null; // input array is not what has been described
    }
}
class Solution {
    public int repeatedNTimes(int[] A) {
        Set<Integer> set = new HashSet<>();
        for (int a: A) {
            if (set.contains(a)) {
                return a;
            }
            set.add(a);   
        }
        throw null; // input array is not what has been described
    }
}

O(N) complexity and O(N) space by using a set data structure.

Random

If we randomly pick two different indices, there is a high chance that the numbers on them will be the same – the majority of the numbers are duplicate (half). We run forever generating two random different indices and compare the values until they are the same – which we have the answer!

1
2
3
4
5
6
7
8
class Solution {
    public int repeatedNTimes(int[] A) {
        Random random = new Random();
        int i, j;
        while ((A[i = random.nextInt(A.length)]) != (A[j = random.nextInt(A.length)]) || i == j  );
        return A[i];
    }
}
class Solution {
    public int repeatedNTimes(int[] A) {
        Random random = new Random();
        int i, j;
        while ((A[i = random.nextInt(A.length)]) != (A[j = random.nextInt(A.length)]) || i == j  );
        return A[i];
    }
}

This algorithm usually runs in constant time – assuming we have a good random number generator. The space complexity is O(1).

Pigeon Holes Algorithm

Those N repeative number could be either placed evenly or before/after other N numbers – the pigeon holes principle.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
    public int repeatedNTimes(int[] A) {
        for (int i = 0; i < A.length - 1; ++ i) {
            if (A[i] == A[i + 1]) { // any two neighbour numbers 
                return A[i];
            }            
        }
        // could be evenly distributed excluding the above case
        for (int i = 0; i < A.length - 2; ++ i) {
            if (A[i] == A[A.length - 1] || A[i] == A[A.length - 2]) {
                return A[i];
            }
        }
        throw null; // input array is not what has been described
    }
}
class Solution {
    public int repeatedNTimes(int[] A) {
        for (int i = 0; i < A.length - 1; ++ i) {
            if (A[i] == A[i + 1]) { // any two neighbour numbers 
                return A[i];
            }            
        }
        // could be evenly distributed excluding the above case
        for (int i = 0; i < A.length - 2; ++ i) {
            if (A[i] == A[A.length - 1] || A[i] == A[A.length - 2]) {
                return A[i];
            }
        }
        throw null; // input array is not what has been described
    }
}

The above algorithm runs in O(N) time and O(1) constant space complexity.

For C++ and other solutions, please visit GITHUB.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
翡翠台直播-TVB翡翠台在线直播「高清」  凤凰卫视中文台在线直播「高清」  凤凰卫视资讯台直播-凤凰资讯台在线直播「高清」  星空卫视直播-星空卫视在线直播观看「高清」  健康卫视直播-健康卫视在线直播观看「高清」  莲花卫视直播-莲花卫视在线直播观看「高清」  澳亚卫视直播-澳亚卫视在线直播观看「高清」  阳光卫视直播-阳光卫视在线直播观看「高清」  金鹰卡通直播-金鹰卡通在线直播观看「高清」  优漫卡通直播-优漫卡通卫视在线直播「高清」 
评论列表
添加评论