
- 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
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 Articles
- Find Array Elements Which are Greater Than Its Immediate Left Element?
- Python – Find the frequency of numbers greater than each element in a list
- Find Two Array Elements Having Maximum Product in Java?
- Find Two Array Elements Having Maximum Sum in Java?
- Find frequency of each element in a limited range array in less than O(n) time in C++
- Divide every element of one array by other array elements in C++ Program
- Find element in a sorted array whose frequency is greater than or equal to n/2 in C++.
- Elements greater than the previous and next element in an Array in C++
- Find the only different element in an array using C++
- Find Duplicate Elements and its Frequency in an Array in Java
- Find Array Elements Which are Greater than its Left Elements in Java?
- 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
- 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++
