- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 duplicates under given constraints in C++
Suppose we have a list with 6 different numbers. Only one number is repeated five times. So there are total 10 elements in the array. Find duplicate numbers using only two comparisons. If the list is like [1, 2, 3, 4, 4, 4, 4, 4, 5, 6], so output is 4.
As there are only 10 numbers, then for any type of duplicate numbers, the range of numbers will be placed from index 3 to 5. By checking these indices, we can find the result.
Example
#include<iostream> using namespace std; int getDuplicate(int array[]) { if (array[3] == array[4]) return array[3]; else if (array[4] == array[5]) return array[4]; else return array[5]; } int main() { int a[] = {1, 2, 3, 4, 4, 4, 4, 4, 5, 6}; cout << "Duplicate element: " << getDuplicate(a); }
Output
Duplicate element: 4
- Related Articles
- Find the longest path in a matrix with given constraints in C++
- Find minimum time to finish all jobs with given constraints in C++
- C++ program to find last value of matrix with given constraints
- Find minimum time to finish all jobs with given constraints in Python
- Remove all duplicates from a given string in C#
- Add elements of given arrays with given constraints?
- Add the elements of given arrays with given constraints?
- Find duplicates in a given array when elements are not limited to a range in C++
- Find All Duplicates in an Array in C++
- Maximum number of ones in a N*N matrix with given constraints in C++
- Find the final X and Y when they are Altering under given condition in C++
- Find Duplicates of array using bit array in C++
- Find the final X and Y when they are Altering under given condition in C++ Programming
- Print all distinct permutations of a given string with duplicates in C++
- C++ code to find a point that satisfies the constraints

Advertisements