
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Unique element in an array where all elements occur k times except one in C++
we have an array A. A has all elements occurring m times, but one element occurs only once. We have to find that unique element.
So, if the input is like A = [6, 2, 7, 2, 2, 6, 6], m = 3, then the output will be 7.
To solve this, we will follow these steps −
- INT_SIZE := 8 * size of an integer type variable
- Define an array count of size: INT_SIZE. and fill with 0
- for initialize i := 0, when i < INT_SIZE, update (increase i by 1), do −
- for initialize j := 0, when j < size, update (increase j by 1), do −
- if (arr[j] AND 2^i) is not equal to 0, then −
- count[i] := count[i] + 1
- res := 0
- if (arr[j] AND 2^i) is not equal to 0, then −
- for initialize i := 0, when i < INT_SIZE, update (increase i by 1), do −
- res := res + ((count[i] mod m) * 2^i)
- return res
- for initialize j := 0, when j < size, update (increase j by 1), do −
Let us see the following implementation to get better understanding −
Example (C++)
#include <bits/stdc++.h> using namespace std; int selectUnique(unsigned int arr[], int size, int m){ int INT_SIZE = 8 * sizeof(unsigned int); int count[INT_SIZE]; memset(count, 0, sizeof(count)); for(int i = 0; i < INT_SIZE; i++) for(int j = 0; j < size; j++) if((arr[j] & (1 << i)) != 0) count[i] += 1; unsigned res = 0; for(int i = 0; i < INT_SIZE; i++) res += (count[i] % m) * (1 << i); return res; } main(){ unsigned int arr[] = { 6, 2, 5, 2, 2, 6, 6 }; int size = sizeof(arr) / sizeof(arr[0]); int m = 3; cout << selectUnique(arr, size, m); }
Input
{ 6, 2, 5, 2, 2, 6, 6 }
Output
5
- Related Articles
- Construct an array from XOR of all elements of array except element at same index in C++
- Find an integer X which is divisor of all except exactly one element in an array in C++
- Find an integer X which is divisor of all except exactly one element in an array in Python
- Find the K-th minimum element from an array concatenated M times in C++
- Product of all Subsequences of size K except the minimum and maximum Elements in C++
- Count all elements in the array which appears at least K times after their first occurrence in C++
- C program to find the unique elements in an array.
- How to find all elements in a given array except for the first one using JavaScript?
- Mask array elements where invalid values NaNs or infs occur in Numpy
- Counting unique elements in an array in JavaScript
- k-th missing element in an unsorted array in C++
- Product of all the elements in an array divisible by a given number K in C++
- Maximum Unique Element in every subarray of size K in c++
- Divide every element of one array by other array elements in C++ Program
- Adding elements of an array until every element becomes greater than or equal to k in C++.

Advertisements