📌  相关文章
📜  数组中的唯一元素,其中所有元素均出现k次(一次除外)

📅  最后修改于: 2021-05-25 06:22:30             🧑  作者: Mango

给定一个包含所有元素出现k次的数组,但一个元素仅出现一次。找到那个独特的元素。
例子:

Input  : arr[] = {6, 2, 5, 2, 2, 6, 6}
            k = 3
Output : 5
Every element appears 3 times accept 5.

Input  : arr[] = {2, 2, 2, 10, 2}
            k = 4
Output : 10
Every element appears 4 times accept 10.

一个简单的解决方案是使用两个嵌套循环。外循环从最左边的元素开始一个接一个地选择一个元素。内部循环检查元素是否存在k次。如果存在,则忽略该元素,否则打印该元素。
上述解决方案的时间复杂度为O(n 2 )。我们可以使用排序来解决O(nLogn)时间中的问题。这个想法很简单,首先对数组进行排序,以便每个元素的所有出现变为连续。一旦出现连续的情况,我们就可以遍历排序后的数组并在O(n)的时间内打印出唯一的元素。
我们可以使用散列来平均解决O(n)时间。这个想法是从左到右遍历给定的数组,并在哈希表中跟踪访问的元素。最后打印计数为1的元素。
基于哈希的解决方案需要O(n)额外的空间。我们可以使用按位AND在O(n)时间和恒定的额外空间中找到唯一元素。

  1. 创建一个数组count [] ,其大小等于数字的二进制表示形式中的位数。
  2. 填充计数数组,以便count [i]存储设置了第i位的数组元素的计数。
  3. 使用count数组形成结果。如果count [i]不为k的倍数,则将1放置在结果i中。否则,我们输入0。
C++
// CPP program to find unique element where
// every element appears k times except one
#include 
using namespace std;
 
int findUnique(unsigned int a[], int n, int k)
{
    // Create a count array to store count of
    // numbers that have a particular bit set.
    // count[i] stores count of array elements
    // with i-th bit set.
    int INT_SIZE = 8 * sizeof(unsigned int);
    int count[INT_SIZE];
    memset(count, 0, sizeof(count));
 
    // AND(bitwise) each element of the array
    // with each set digit (one at a time)
    // to get the count of set bits at each
    // position
    for (int i = 0; i < INT_SIZE; i++)
        for (int j = 0; j < n; j++)
            if ((a[j] & (1 << i)) != 0)
                count[i] += 1;
 
    // Now consider all bits whose count is
    // not multiple of k to form the required
    // number.
    unsigned res = 0;
    for (int i = 0; i < INT_SIZE; i++)
        res += (count[i] % k) * (1 << i);
    return res;
}
 
// Driver Code
int main()
{
    unsigned int a[] = { 6, 2, 5, 2, 2, 6, 6 };
    int n = sizeof(a) / sizeof(a[0]);
    int k = 3;
    cout << findUnique(a, n, k);
    return 0;
}


Java
// Java program to find unique element where
// every element appears k times except one
 
class GFG
{
     
static int findUnique(int a[], int n, int k)
{
    // Create a count array to store count of
    // numbers that have a particular bit set.
    // count[i] stores count of array elements
    // with i-th bit set.
    byte sizeof_int = 4;
    int INT_SIZE = 8 * sizeof_int;
    int count[] = new int[INT_SIZE];
 
    // AND(bitwise) each element of the array
    // with each set digit (one at a time)
    // to get the count of set bits at each
    // position
    for (int i = 0; i < INT_SIZE; i++)
        for (int j = 0; j < n; j++)
            if ((a[j] & (1 << i)) != 0)
                count[i] += 1;
 
    // Now consider all bits whose count is
    // not multiple of k to form the required
    // number.
    int res = 0;
    for (int i = 0; i < INT_SIZE; i++)
        res += (count[i] % k) * (1 << i);
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
    int a[] = { 6, 2, 5, 2, 2, 6, 6 };
    int n = a.length;
    int k = 3;
    System.out.println(findUnique(a, n, k));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python 3 program to find unique element where
# every element appears k times except one
import sys
 
def findUnique(a, n, k):
     
    # Create a count array to store count of
    # numbers that have a particular bit set.
    # count[i] stores count of array elements
    # with i-th bit set.
    INT_SIZE = 8 * sys.getsizeof(int)
    count = [0] * INT_SIZE
     
    # AND(bitwise) each element of the array
    # with each set digit (one at a time)
    # to get the count of set bits at each
    # position
    for i in range(INT_SIZE):
        for j in range(n):
            if ((a[j] & (1 << i)) != 0):
                count[i] += 1
 
    # Now consider all bits whose count is
    # not multiple of k to form the required
    # number.
    res = 0
    for i in range(INT_SIZE):
        res += (count[i] % k) * (1 << i)
    return res
 
# Driver Code
if __name__ == '__main__':
    a = [6, 2, 5, 2, 2, 6, 6]
    n = len(a)
    k = 3
    print(findUnique(a, n, k));
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to find unique element where
// every element appears k times except one
using System;
 
class GFG
{
static int findUnique(int []a, int n, int k)
{
    // Create a count array to store count of
    // numbers that have a particular bit set.
    // count[i] stores count of array elements
    // with i-th bit set.
    byte sizeof_int = 4;
    int INT_SIZE = 8 * sizeof_int;
    int []count = new int[INT_SIZE];
 
    // AND(bitwise) each element of the array
    // with each set digit (one at a time)
    // to get the count of set bits at each
    // position
    for (int i = 0; i < INT_SIZE; i++)
        for (int j = 0; j < n; j++)
            if ((a[j] & (1 << i)) != 0)
                count[i] += 1;
 
    // Now consider all bits whose count is
    // not multiple of k to form the required
    // number.
    int res = 0;
    for (int i = 0; i < INT_SIZE; i++)
        res += (count[i] % k) * (1 << i);
    return res;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []a = { 6, 2, 5, 2, 2, 6, 6 };
    int n = a.Length;
    int k = 3;
    Console.WriteLine(findUnique(a, n, k));
}
}
 
// This code is contributed by PrinciRaj1992


PHP


Javascript


输出:

5