

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the element having different frequency than other array elements in C++
Suppose we have an array of N numbers, where each element in the array appears same number of times (m times, this is also given) except one element, We have to find this 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
for initialize i := 0, when i < INT_SIZE, update (increase i by 1), do −
res := res + ((count[i] mod m) * 2^i)
return res
Example
Let us see the following implementation to get better understanding −
#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 Questions & Answers
- Python – Find the frequency of numbers greater than each element in a list
- Find the only different element in an array using C++
- Divide every element of one array by other array elements in C++ Program
- Find frequency of each element in a limited range array in less than O(n) time in C++
- Elements greater than the previous and next element in an Array in C++
- Find element in a sorted array whose frequency is greater than or equal to n/2 in C++.
- How to include ghee in my diet other than having them on chapatis?
- Program to find minimum length of first split of an array with smaller elements than other list in Python
- Find the element that appears once in an array where every other element appears twice in C++
- Write a Golang program to find the frequency of an element in an array
- Write a Golang program to find the frequency of each element in an array
- Find original array from encrypted array (An array of sums of other elements) using C++.
- Count of arrays having consecutive element with different values in C++
- Count array elements that divide the sum of all other elements in C++
- Find the number of elements greater than k in a sorted array using C++