给定一个数组,其中每个元素出现三次,但一个元素仅出现一次。查找出现一次的元素。预期的时间复杂度为O(n)和O(1)额外空间。
例子:
Input: arr[] = {12, 1, 12, 3, 12, 1, 1, 2, 3, 3}
Output: 2
In the given array all element appear three times except 2 which appears once.
Input: arr[] = {10, 20, 10, 30, 10, 30, 30}
Output: 20
In the given array all element appear three times except 20 which appears once.
我们可以使用排序在O(nLogn)时间内完成。我们还可以使用散列,它的时间复杂度最差为O(n),但需要额外的空间。
想法是使用按位运算运算符来求解O(n)时间并使用O(1)额外空间的解决方案。该解决方案不像其他基于XOR的解决方案那样容易,因为所有元素在此处均出现奇数次。这个想法是从这里开始的。
对数组中的所有元素运行循环。在每次迭代结束时,请保持以下两个值。
个:第一次出现,第四次出现或第七次出现的位..等等。
二进制:第2次,第5次或第8次出现的位..等等。
最后,我们返回“ ones”的值
如何保持“一”和“二”的价值?
“ ones”和“ twos”初始化为0。对于数组中的每个新元素,找出新元素中的公共设置位和“ ones”的先前值。这些公共置位实际上是应该加到“ 2”的位。因此,将通用置位与“二进制”进行按位或运算。 “ Twos”还获得了一些第三次出现的额外位。这些多余的位将在以后删除。
通过对新元素与先前值“ ones”进行XOR来更新“ ones”。可能有些位第三次出现。这些额外的位也将在以后删除。
“ 1”和“ 2”都包含第三次出现的那些额外的位。通过找出“ 1”和“ 2”中的公共置位来除去这些额外的位。
下面是上述方法的实现:
C++
// C++ program to find the element
// that occur only once
#include
using namespace std;
int getSingle(int arr[], int n)
{
int ones = 0, twos = 0;
int common_bit_mask;
// Let us take the example of
// {3, 3, 2, 3} to understand
// this
for (int i = 0; i < n; i++) {
/* The expression "one & arr[i]" gives the bits that
are there in both 'ones' and new element from arr[].
We add these bits to 'twos' using bitwise OR
Value of 'twos' will be set as 0, 3, 3 and 1 after
1st, 2nd, 3rd and 4th iterations respectively */
twos = twos | (ones & arr[i]);
/* XOR the new bits with previous 'ones' to get all
bits appearing odd number of times
Value of 'ones' will be set as 3, 0, 2 and 3 after
1st, 2nd, 3rd and 4th iterations respectively */
ones = ones ^ arr[i];
/* The common bits are those bits which appear third
time So these bits should not be there in both
'ones' and 'twos'. common_bit_mask contains all
these bits as 0, so that the bits can be removed
from 'ones' and 'twos'
Value of 'common_bit_mask' will be set as 00, 00, 01
and 10 after 1st, 2nd, 3rd and 4th iterations
respectively */
common_bit_mask = ~(ones & twos);
/* Remove common bits (the bits that appear third
time) from 'ones'
Value of 'ones' will be set as 3, 0, 0 and 2 after
1st, 2nd, 3rd and 4th iterations respectively */
ones &= common_bit_mask;
/* Remove common bits (the bits that appear third
time) from 'twos'
Value of 'twos' will be set as 0, 3, 1 and 0 after
1st, 2nd, 3rd and 4th itearations respectively */
twos &= common_bit_mask;
// uncomment this code to see intermediate values
// printf (" %d %d n", ones, twos);
}
return ones;
}
// Driver code
int main()
{
int arr[] = { 3, 3, 2, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "The element with single occurrence is "
<< getSingle(arr, n);
return 0;
}
// This code is contributed by rathbhupendra
C
// C program to find the element
// that occur only once
#include
int getSingle(int arr[], int n)
{
int ones = 0, twos = 0;
int common_bit_mask;
// Let us take the example of {3, 3, 2, 3} to understand this
for (int i = 0; i < n; i++) {
/* The expression "one & arr[i]" gives the bits that are
there in both 'ones' and new element from arr[]. We
add these bits to 'twos' using bitwise OR
Value of 'twos' will be set as 0, 3, 3 and 1 after 1st,
2nd, 3rd and 4th iterations respectively */
twos = twos | (ones & arr[i]);
/* XOR the new bits with previous 'ones' to get all bits
appearing odd number of times
Value of 'ones' will be set as 3, 0, 2 and 3 after 1st,
2nd, 3rd and 4th iterations respectively */
ones = ones ^ arr[i];
/* The common bits are those bits which appear third time
So these bits should not be there in both 'ones' and 'twos'.
common_bit_mask contains all these bits as 0, so that the bits can
be removed from 'ones' and 'twos'
Value of 'common_bit_mask' will be set as 00, 00, 01 and 10
after 1st, 2nd, 3rd and 4th iterations respectively */
common_bit_mask = ~(ones & twos);
/* Remove common bits (the bits that appear third time) from 'ones'
Value of 'ones' will be set as 3, 0, 0 and 2 after 1st,
2nd, 3rd and 4th iterations respectively */
ones &= common_bit_mask;
/* Remove common bits (the bits that appear third time) from 'twos'
Value of 'twos' will be set as 0, 3, 1 and 0 after 1st,
2nd, 3rd and 4th itearations respectively */
twos &= common_bit_mask;
// uncomment this code to see intermediate values
// printf (" %d %d n", ones, twos);
}
return ones;
}
int main()
{
int arr[] = { 3, 3, 2, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("The element with single occurrence is %d ",
getSingle(arr, n));
return 0;
}
Java
// Java code to find the element
// that occur only once
class GFG {
// Method to find the element that occur only once
static int getSingle(int arr[], int n)
{
int ones = 0, twos = 0;
int common_bit_mask;
for (int i = 0; i < n; i++) {
/*"one & arr[i]" gives the bits that are there in
both 'ones' and new element from arr[]. We
add these bits to 'twos' using bitwise OR*/
twos = twos | (ones & arr[i]);
/*"one & arr[i]" gives the bits that are
there in both 'ones' and new element from arr[].
We add these bits to 'twos' using bitwise OR*/
ones = ones ^ arr[i];
/* The common bits are those bits which appear third time
So these bits should not be there in both 'ones' and 'twos'.
common_bit_mask contains all these bits as 0, so that the bits can
be removed from 'ones' and 'twos'*/
common_bit_mask = ~(ones & twos);
/*Remove common bits (the bits that appear third time) from 'ones'*/
ones &= common_bit_mask;
/*Remove common bits (the bits that appear third time) from 'twos'*/
twos &= common_bit_mask;
}
return ones;
}
// Driver method
public static void main(String args[])
{
int arr[] = { 3, 3, 2, 3 };
int n = arr.length;
System.out.println("The element with single occurrence is " + getSingle(arr, n));
}
}
// Code contributed by Rishab Jain
Python3
# Python3 code to find the element that
# appears once
def getSingle(arr, n):
ones = 0
twos = 0
for i in range(n):
# one & arr[i]" gives the bits that
# are there in both 'ones' and new
# element from arr[]. We add these
# bits to 'twos' using bitwise OR
twos = twos | (ones & arr[i])
# one & arr[i]" gives the bits that
# are there in both 'ones' and new
# element from arr[]. We add these
# bits to 'twos' using bitwise OR
ones = ones ^ arr[i]
# The common bits are those bits
# which appear third time. So these
# bits should not be there in both
# 'ones' and 'twos'. common_bit_mask
# contains all these bits as 0, so
# that the bits can be removed from
# 'ones' and 'twos'
common_bit_mask = ~(ones & twos)
# Remove common bits (the bits that
# appear third time) from 'ones'
ones &= common_bit_mask
# Remove common bits (the bits that
# appear third time) from 'twos'
twos &= common_bit_mask
return ones
# driver code
arr = [3, 3, 2, 3]
n = len(arr)
print("The element with single occurrence is ",
getSingle(arr, n))
# This code is contributed by "Abhishek Sharma 44"
C#
// C# code to find the element
// that occur only once
using System;
class GFG {
// Method to find the element
// that occur only once
static int getSingle(int[] arr, int n)
{
int ones = 0, twos = 0;
int common_bit_mask;
for (int i = 0; i < n; i++) {
// "one & arr[i]" gives the bits
// that are there in both 'ones'
// and new element from arr[].
// We add these bits to 'twos'
// using bitwise OR
twos = twos | (ones & arr[i]);
// "one & arr[i]" gives the bits
// that are there in both 'ones'
// and new element from arr[].
// We add these bits to 'twos'
// using bitwise OR
ones = ones ^ arr[i];
// The common bits are those bits
// which appear third time So these
// bits should not be there in both
// 'ones' and 'twos'. common_bit_mask
// contains all these bits as 0,
// so that the bits can be removed
// from 'ones' and 'twos'
common_bit_mask = ~(ones & twos);
// Remove common bits (the bits that
// appear third time) from 'ones'
ones &= common_bit_mask;
// Remove common bits (the bits that
// appear third time) from 'twos'
twos &= common_bit_mask;
}
return ones;
}
// Driver code
public static void Main()
{
int[] arr = { 3, 3, 2, 3 };
int n = arr.Length;
Console.WriteLine("The element with single"
+ "occurrence is " + getSingle(arr, n));
}
}
// This Code is contributed by vt_m.
PHP
Javascript
C++
// C++ program to find the element
// that occur only once
#include
using namespace std;
#define INT_SIZE 32
int getSingle(int arr[], int n)
{
// Initialize result
int result = 0;
int x, sum;
// Iterate through every bit
for (int i = 0; i < INT_SIZE; i++) {
// Find sum of set bits at ith position in all
// array elements
sum = 0;
x = (1 << i);
for (int j = 0; j < n; j++) {
if (arr[j] & x)
sum++;
}
// The bits with sum not multiple of 3, are the
// bits of element with single occurrence.
if ((sum % 3) != 0)
result |= x;
}
return result;
}
// Driver code
int main()
{
int arr[] = { 12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "The element with single occurrence is " << getSingle(arr, n);
return 0;
}
// This code is contributed by rathbhupendra
C
// C program to find the element
// that occur only once
#include
#define INT_SIZE 32
int getSingle(int arr[], int n)
{
// Initialize result
int result = 0;
int x, sum;
// Iterate through every bit
for (int i = 0; i < INT_SIZE; i++) {
// Find sum of set bits at ith position in all
// array elements
sum = 0;
x = (1 << i);
for (int j = 0; j < n; j++) {
if (arr[j] & x)
sum++;
}
// The bits with sum not multiple of 3, are the
// bits of element with single occurrence.
if ((sum % 3) != 0)
result |= x;
}
return result;
}
// Driver program to test above function
int main()
{
int arr[] = { 12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("The element with single occurrence is %d ",
getSingle(arr, n));
return 0;
}
Java
// Java code to find the element
// that occur only once
class GFG {
static final int INT_SIZE = 32;
// Method to find the element that occur only once
static int getSingle(int arr[], int n)
{
int result = 0;
int x, sum;
// Iterate through every bit
for (int i = 0; i < INT_SIZE; i++) {
// Find sum of set bits at ith position in all
// array elements
sum = 0;
x = (1 << i);
for (int j = 0; j < n; j++) {
if ((arr[j] & x) == 0)
sum++;
}
// The bits with sum not multiple of 3, are the
// bits of element with single occurrence.
if ((sum % 3) != 0)
result |= x;
}
return result;
}
// Driver method
public static void main(String args[])
{
int arr[] = { 12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7 };
int n = arr.length;
System.out.println("The element with single occurrence is " + getSingle(arr, n));
}
}
// Code contributed by Rishab Jain
Python 3
# Python3 code to find the element
# that occur only once
INT_SIZE = 32
def getSingle(arr, n) :
# Initialize result
result = 0
# Iterate through every bit
for i in range(0, INT_SIZE) :
# Find sum of set bits
# at ith position in all
# array elements
sm = 0
x = (1 << i)
for j in range(0, n) :
if (arr[j] & x) :
sm = sm + 1
# The bits with sum not
# multiple of 3, are the
# bits of element with
# single occurrence.
if ((sm % 3)!= 0) :
result = result | x
return result
# Driver program
arr = [12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7]
n = len(arr)
print("The element with single occurrence is ", getSingle(arr, n))
# This code is contributed
# by Nikita Tiwari.
C#
// C# code to find the element
// that occur only once
using System;
class GFG {
static int INT_SIZE = 32;
// Method to find the element
// that occur only once
static int getSingle(int[] arr, int n)
{
int result = 0;
int x, sum;
// Iterate through every bit
for (int i = 0; i < INT_SIZE; i++) {
// Find sum of set bits at ith
// position in all array elements
sum = 0;
x = (1 << i);
for (int j = 0; j < n; j++) {
if ((arr[j] & x) == 0)
sum++;
}
// The bits with sum not multiple
// of 3, are the bits of element
// with single occurrence.
if ((sum % 3) != 0)
result |= x;
}
return result;
}
// Driver Code
public static void Main()
{
int[] arr = { 12, 1, 12, 3, 12, 1,
1, 2, 3, 2, 2, 3, 7 };
int n = arr.Length;
Console.WriteLine("The element with single "
+ "occurrence is " + getSingle(arr, n));
}
}
// This code is contributed ny vt_m.
PHP
Javascript
C++
// C++ program to find the element
// that occur only once
#include
using namespace std;
// function which find number
int singleNumber(int a[], int n)
{
unordered_set s(a, a + n);
int arr_sum = accumulate(a, a + n, 0); // sum of array
int set_sum = accumulate(s.begin(), s.end(), 0); // sum of set
// applying the formula.
return (3 * set_sum - arr_sum) / 2;
}
// driver function
int main()
{
int a[] = { 12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7 };
int n = sizeof(a) / sizeof(a[0]);
cout << "The element with single occurrence is " << singleNumber(a, n);
}
// This code is contributed by Mohit Kumar 29 (IIIT gwalior)
Java
// Java program to find the element
// that occur only once
import java.util.*;
class GFG {
// function which find number
static int singleNumber(int a[], int n)
{
HashSet s = new HashSet();
for (int i : a) {
s.add(i);
}
int arr_sum = 0; // sum of array
for (int i : a) {
arr_sum += i;
}
int set_sum = 0; // sum of set
for (int i : s) {
set_sum += i;
}
// applying the formula.
return (3 * set_sum - arr_sum) / 2;
}
// Driver code
public static void main(String[] args)
{
int a[] = { 12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7 };
int n = a.length;
System.out.println("The element with single "
+ "occurrence is " + singleNumber(a, n));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to find the element
# that occur only once
# function which find number
def singleNumber(nums):
# applying the formula.
return (3 * sum(set(nums)) - sum(nums)) / 2
# driver function.
a = [12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7]
print ("The element with single occurrence is ",
int(singleNumber(a)))
C#
// C# program to find the element
// that occur only once
using System;
using System.Collections.Generic;
class GFG {
// function which find number
static int singleNumber(int[] a, int n)
{
HashSet s = new HashSet();
foreach(int i in a)
{
s.Add(i);
}
int arr_sum = 0; // sum of array
foreach(int i in a)
{
arr_sum += i;
}
int set_sum = 0; // sum of set
foreach(int i in s)
{
set_sum += i;
}
// applying the formula.
return (3 * set_sum - arr_sum) / 2;
}
// Driver code
public static void Main(String[] args)
{
int[] a = { 12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7 };
int n = a.Length;
Console.WriteLine("The element with single "
+ "occurrence is " + singleNumber(a, n));
}
}
// This code is contributed by PrinciRaj1992
PHP
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// function which find number
int singlenumber(int a[],int N)
{
// umap for finding frequency
unordered_mapfmap;
// traverse the array for frequency
for(int i = 0; i < N;i++)
{
fmap[a[i]]++;
}
// iterate over the map
for(auto it:fmap)
{
// check frequency whether it is one or not.
if(it.second == 1)
{
// return it as we got the answer
return it.first;
}
}
}
// Driver code
int main()
{
// given array
int a[]={12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7};
// size of the array
int N=sizeof(a)/sizeof(a[0]);
// printing the returned value
cout << singlenumber(a,N);
return 0;
}
// This Code is contributed by
// Murarishetty Santhosh Charan
Python3
from collections import Counter
# Python3 program to find the element
# that occur only once
# function which find number
def singleNumber(nums):
# storing the frequencies using Counter
freq = Counter(nums)
# traversing the Counter dictionary
for i in freq:
# check if any value is 1
if(freq[i] == 1):
return i
# driver function.
a = [12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7]
print("The element with single occurrence is ",
int(singleNumber(a)))
# This code is contributed by vikkycirus
The element with single occurrence is 2
时间复杂度: O(n)
辅助空间: O(1)
以下是aj建议的另一种O(n)时间复杂度和O(1)额外空间方法。我们可以对所有数字的相同位置的位求和,并对3取模。总和不是3的倍数的位是一次出现的数字位。
让我们考虑示例数组{5,5,5,8}。 101、101、101、1000
前几位的总和%3 =(1 + 1 + 1 + 0)%3 = 0;
第二个位的总和%3 =(0 + 0 + 0 + 0)%0 = 0;
第三位的总和%3 =(1 + 1 + 1 + 0)%3 = 0;
第四位的总和%3 =(1)%3 = 1;
因此,一次出现的数字是1000
注意:此方法不适用于负数
下面是上述方法的实现:
C++
// C++ program to find the element
// that occur only once
#include
using namespace std;
#define INT_SIZE 32
int getSingle(int arr[], int n)
{
// Initialize result
int result = 0;
int x, sum;
// Iterate through every bit
for (int i = 0; i < INT_SIZE; i++) {
// Find sum of set bits at ith position in all
// array elements
sum = 0;
x = (1 << i);
for (int j = 0; j < n; j++) {
if (arr[j] & x)
sum++;
}
// The bits with sum not multiple of 3, are the
// bits of element with single occurrence.
if ((sum % 3) != 0)
result |= x;
}
return result;
}
// Driver code
int main()
{
int arr[] = { 12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "The element with single occurrence is " << getSingle(arr, n);
return 0;
}
// This code is contributed by rathbhupendra
C
// C program to find the element
// that occur only once
#include
#define INT_SIZE 32
int getSingle(int arr[], int n)
{
// Initialize result
int result = 0;
int x, sum;
// Iterate through every bit
for (int i = 0; i < INT_SIZE; i++) {
// Find sum of set bits at ith position in all
// array elements
sum = 0;
x = (1 << i);
for (int j = 0; j < n; j++) {
if (arr[j] & x)
sum++;
}
// The bits with sum not multiple of 3, are the
// bits of element with single occurrence.
if ((sum % 3) != 0)
result |= x;
}
return result;
}
// Driver program to test above function
int main()
{
int arr[] = { 12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("The element with single occurrence is %d ",
getSingle(arr, n));
return 0;
}
Java
// Java code to find the element
// that occur only once
class GFG {
static final int INT_SIZE = 32;
// Method to find the element that occur only once
static int getSingle(int arr[], int n)
{
int result = 0;
int x, sum;
// Iterate through every bit
for (int i = 0; i < INT_SIZE; i++) {
// Find sum of set bits at ith position in all
// array elements
sum = 0;
x = (1 << i);
for (int j = 0; j < n; j++) {
if ((arr[j] & x) == 0)
sum++;
}
// The bits with sum not multiple of 3, are the
// bits of element with single occurrence.
if ((sum % 3) != 0)
result |= x;
}
return result;
}
// Driver method
public static void main(String args[])
{
int arr[] = { 12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7 };
int n = arr.length;
System.out.println("The element with single occurrence is " + getSingle(arr, n));
}
}
// Code contributed by Rishab Jain
的Python 3
# Python3 code to find the element
# that occur only once
INT_SIZE = 32
def getSingle(arr, n) :
# Initialize result
result = 0
# Iterate through every bit
for i in range(0, INT_SIZE) :
# Find sum of set bits
# at ith position in all
# array elements
sm = 0
x = (1 << i)
for j in range(0, n) :
if (arr[j] & x) :
sm = sm + 1
# The bits with sum not
# multiple of 3, are the
# bits of element with
# single occurrence.
if ((sm % 3)!= 0) :
result = result | x
return result
# Driver program
arr = [12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7]
n = len(arr)
print("The element with single occurrence is ", getSingle(arr, n))
# This code is contributed
# by Nikita Tiwari.
C#
// C# code to find the element
// that occur only once
using System;
class GFG {
static int INT_SIZE = 32;
// Method to find the element
// that occur only once
static int getSingle(int[] arr, int n)
{
int result = 0;
int x, sum;
// Iterate through every bit
for (int i = 0; i < INT_SIZE; i++) {
// Find sum of set bits at ith
// position in all array elements
sum = 0;
x = (1 << i);
for (int j = 0; j < n; j++) {
if ((arr[j] & x) == 0)
sum++;
}
// The bits with sum not multiple
// of 3, are the bits of element
// with single occurrence.
if ((sum % 3) != 0)
result |= x;
}
return result;
}
// Driver Code
public static void Main()
{
int[] arr = { 12, 1, 12, 3, 12, 1,
1, 2, 3, 2, 2, 3, 7 };
int n = arr.Length;
Console.WriteLine("The element with single "
+ "occurrence is " + getSingle(arr, n));
}
}
// This code is contributed ny vt_m.
的PHP
Java脚本
The element with single occurrence is 7
Abhishek Sharma建议的另一种方法44 。将每个数字相加一次,然后将总和乘以3,我们将得到数组每个元素总和的三次。将其存储为thrice_sum。从thrice_sum中减去整个数组的总和,然后将结果除以2。我们得到的数字是所需的数字(在数组中出现一次)。
数组[]:[a,a,a,b,b,b,c,c,c,d]
数学方程=(3 *(a + b + c + d)–(a + a + a + b + b + b + c + c + c + d))/ 2
用更简单的话说:(3 *(sum_of_array_without_duplicates)–(sum_of_array))/ 2
let arr[] = {12, 1, 12, 3, 12, 1, 1, 2, 3, 3}
Required no = ( 3*(sum_of_array_without_duplicates) - (sum_of_array) ) / 2
= ( 3*(12 + 1 + 3 + 2) - (12 + 1 + 12 + 3 + 12 + 1 + 1 + 2 + 3 + 3))/2
= ( 3* 18 - 50) / 2
= (54 - 50) / 2
= 2 (required answer)
我们知道set不包含任何重复元素,
但是,std :: set通常实现为红黑色二进制搜索树。由于树保持平衡,因此在此数据结构上插入会带来O(log(n))复杂度的最坏情况。我们将在这里使用set。
下面是上述方法的实现:
C++
// C++ program to find the element
// that occur only once
#include
using namespace std;
// function which find number
int singleNumber(int a[], int n)
{
unordered_set s(a, a + n);
int arr_sum = accumulate(a, a + n, 0); // sum of array
int set_sum = accumulate(s.begin(), s.end(), 0); // sum of set
// applying the formula.
return (3 * set_sum - arr_sum) / 2;
}
// driver function
int main()
{
int a[] = { 12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7 };
int n = sizeof(a) / sizeof(a[0]);
cout << "The element with single occurrence is " << singleNumber(a, n);
}
// This code is contributed by Mohit Kumar 29 (IIIT gwalior)
Java
// Java program to find the element
// that occur only once
import java.util.*;
class GFG {
// function which find number
static int singleNumber(int a[], int n)
{
HashSet s = new HashSet();
for (int i : a) {
s.add(i);
}
int arr_sum = 0; // sum of array
for (int i : a) {
arr_sum += i;
}
int set_sum = 0; // sum of set
for (int i : s) {
set_sum += i;
}
// applying the formula.
return (3 * set_sum - arr_sum) / 2;
}
// Driver code
public static void main(String[] args)
{
int a[] = { 12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7 };
int n = a.length;
System.out.println("The element with single "
+ "occurrence is " + singleNumber(a, n));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to find the element
# that occur only once
# function which find number
def singleNumber(nums):
# applying the formula.
return (3 * sum(set(nums)) - sum(nums)) / 2
# driver function.
a = [12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7]
print ("The element with single occurrence is ",
int(singleNumber(a)))
C#
// C# program to find the element
// that occur only once
using System;
using System.Collections.Generic;
class GFG {
// function which find number
static int singleNumber(int[] a, int n)
{
HashSet s = new HashSet();
foreach(int i in a)
{
s.Add(i);
}
int arr_sum = 0; // sum of array
foreach(int i in a)
{
arr_sum += i;
}
int set_sum = 0; // sum of set
foreach(int i in s)
{
set_sum += i;
}
// applying the formula.
return (3 * set_sum - arr_sum) / 2;
}
// Driver code
public static void Main(String[] args)
{
int[] a = { 12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7 };
int n = a.Length;
Console.WriteLine("The element with single "
+ "occurrence is " + singleNumber(a, n));
}
}
// This code is contributed by PrinciRaj1992
的PHP
Java脚本
The element with single occurrence is 7
时间复杂度: O(Nlog(N))
辅助空间: O(N)
方法4:使用Counter()函数
- 使用Counter函数计算数组的频率
- 遍历此Counter字典并检查是否有任何键的值为1
- 如果任何键的值为1,则返回键
下面是实现:
C++
// C++ program for the above approach
#include
using namespace std;
// function which find number
int singlenumber(int a[],int N)
{
// umap for finding frequency
unordered_mapfmap;
// traverse the array for frequency
for(int i = 0; i < N;i++)
{
fmap[a[i]]++;
}
// iterate over the map
for(auto it:fmap)
{
// check frequency whether it is one or not.
if(it.second == 1)
{
// return it as we got the answer
return it.first;
}
}
}
// Driver code
int main()
{
// given array
int a[]={12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7};
// size of the array
int N=sizeof(a)/sizeof(a[0]);
// printing the returned value
cout << singlenumber(a,N);
return 0;
}
// This Code is contributed by
// Murarishetty Santhosh Charan
Python3
from collections import Counter
# Python3 program to find the element
# that occur only once
# function which find number
def singleNumber(nums):
# storing the frequencies using Counter
freq = Counter(nums)
# traversing the Counter dictionary
for i in freq:
# check if any value is 1
if(freq[i] == 1):
return i
# driver function.
a = [12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7]
print("The element with single occurrence is ",
int(singleNumber(a)))
# This code is contributed by vikkycirus
The element with single occurrence is 7
时间复杂度: O(n)