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

📅  最后修改于: 2021-10-27 08:40:59             🧑  作者: 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) 额外空间。我们可以使用按位与在 O(n) 时间和恒定额外空间内找到唯一元素。

  1. 创建一个数组count[] ,其大小等于数字的二进制表示中的位数。
  2. 填充 count 数组,使得 count[i] 存储第 i 位设置的数组元素的计数。
  3. 使用计数数组形成结果。如果 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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程