Find the duplicate in an array of N integers.
C++
/*This method is all in one
*you can find following things:
*finding Duplicate elements in array
*array without duplicate elements
*number of duplicate elements
*numbers of pair of dulicate with repeatation
*/
//let given array = [2,3,2,5,3]
public static void findDuplicateArray(int [] array)
{
int size = array.length;
//creating array to hold count frequency of array elements
int [] countFrequency = new int[size];
// filling countFrequency with -1 value on every index
for(int i = 0; i < size; i++)
{
countFrequency[i] = -1;//[-1,-1,-1,-1,-1...]
}
int count = 1;
for(int i = 0; i < size; i++)
{
//check countFrequency[i] != 0 because 0 means it already counted
if(countFrequency[i] != 0)
{
for(int j = i+1; j < size; j++)
{
//if array[i] == array[j] then increase count value
if(array[i] == array[j])
{
count++;
/*only at first occurence of an element count value
*will be increased else everywhere it will be 0
*/
countFrequency[j]= 0;
}
}
countFrequency[i] = count;
}
count = 1;
}
// array = [2,3,2,5,3]
//countFrequency = [2,2,0,1,0]
System.out.println("array without duplicate elements");
for(int i = 0; i < array.length; i++)
{
if(countFrequency[i] >= 1)
System.out.print(array[i] + " ");
}
System.out.println();
System.out.println("duplicate elements in array");
for(int i = 0; i < array.length; i++)
{
if(countFrequency[i]/2 >= 1)
System.out.print(array[i] + " ");
}
System.out.println();
System.out.println("number of duplicate elements");
count = 0;
for(int i = 0; i < array.length; i++)
{
if(countFrequency[i]/2 >= 1)
count++;
}
System.out.print(count);
System.out.println();
System.out.println("numbers of pair of dulicate with repeatation");
count = 0;
for(int i = 0; i < array.length; i++)
{
if(countFrequency[i] >= 2)
{
int div = countFrequency[i]/2;
count+=div;
}
}
System.out.println(count);
int [] array3 = new int [array.length];
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < countFrequency[i]; j++)
{
array3[i]= array[i];
}
}
}// 287. Find the Duplicate Number
// Medium
// Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
// Example 1:
// Input: [1,3,4,2,2]
// Output: 2
// Example 2:
// Input: [3,1,3,4,2]
// Output: 3
// Note:
// You must not modify the array (assume the array is read only).
// You must use only constant, O(1) extra space.
// Your runtime complexity should be less than O(n2).
// There is only one duplicate number in the array, but it could be repeated more than once.
class Solution {
public:
int findDuplicate(vector<int>& nums) {
int n=nums.size();
int s=nums[0];
int f=nums[nums[0]];
while(s!=f) {
s=nums[s];
f=nums[nums[f]];
}
f=0;
while(s!=f) {
s=nums[s];
f=nums[f];
}
return s;
}
};
Also in C++:
- Title
- find all the palindrome substring in a given string
- Category
- C++
- Title
- UPARAM(ref)
- Category
- C++
- Title
- kruskal's algorithm c++ hackerearth
- Category
- C++
- Title
- how to get the largest number in a c++ array
- Category
- C++
- Title
- c++ empty stream
- Category
- C++
- Title
- how to pass an object by reference in c++
- Category
- C++
- Title
- c++ clear stream
- Category
- C++
- Title
- placement new c++
- Category
- C++
- Title
- c++ initialization list
- Category
- C++
- Title
- recursion in cpp with reference
- Category
- C++
- Title
- switch case sinax c++
- Category
- C++
- Title
- how to use wasd c++
- Category
- C++
- Title
- c++ typedef
- Category
- C++
- Title
- how to sort a vector in c++
- Category
- C++
- Title
- count function c++
- Category
- C++
- Title
- FInd the element which appears more than n/2 times C++
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- : error: ‘cont’ cannot be used as a function return (cont(cont-1))/2;
- Category
- C++
- Title
- c++ vector pop_back
- Category
- C++
- Title
- c++ do you not inherit constructor
- Category
- C++
- Title
- set c++
- Category
- C++
- Title
- substr c++
- Category
- C++
- Title
- if vector contains value c++
- Category
- C++
- Title
- how to declare a function in c++
- Category
- C++
- Title
- c++ typeid get type name
- Category
- C++
- Title
- Get handle in C++
- Category
- C++
- Title
- how to write an or in c++
- Category
- C++
- Title
- sort function in cpp
- Category
- C++
- Title
- cs1955 unity vector3
- Category
- C++
- Title
- c++ string to integer without stoi
- Category
- C++
- Title
- popualte an array c++
- Category
- C++
- Title
- how to check type in c++
- Category
- C++
- Title
- length of array in cpp
- Category
- C++
- Title
- what does map.count() return in c++
- Category
- C++
- Title
- object reference not set to an instance of an object c#
- Category
- C++
- Title
- c++ char print align
- Category
- C++
- Title
- how to get os name in c++
- Category
- C++
- Title
- how to remove maximum number of characters in c++ cin,ignore
- Category
- C++
- Title
- fmod c++
- Category
- C++
- Title
- array syntax in c++
- Category
- C++
- Title
- int max in c++
- Category
- C++
- Title
- c++ public inheritance not getting protected
- Category
- C++
- Title
- private and public in namespace cpp
- Category
- C++
- Title
- create a 2d array c++
- Category
- C++
- Title
- git branch in my bash prompt
- Category
- C++
- Title
- error: redefinition of ‘class Customer’
- Category
- C++
- Title
- how to make a heap using stl in c++
- Category
- C++
- Title
- type id c++
- Category
- C++
- Title
- sort a string alphabetically c++
- Category
- C++
- Title
- sorting of array in c++
- Category
- C++
- Title
- Runtime Error: Runtime ErrorBad memory access (SIGBUS)
- Category
- C++
- Title
- how to execute c++ program in cmd
- Category
- C++
- Title
- map insert c++
- Category
- C++
- Title
- c++ files
- Category
- C++
- Title
- c++ lettura file
- Category
- C++
- Title
- comparing strings c++
- Category
- C++
- Title
- double ended queue in c++ stl
- Category
- C++
- Title
- #include
- Category
- C++
- Title
- variabili in c++
- Category
- C++
- Title
- char **
- Category
- C++
- Title
- intersection between vector c++
- Category
- C++
- Title
- c++ pointers
- Category
- C++
- Title
- cpp nan value
- Category
- C++
- Title
- how to make string get spaces c++
- Category
- C++
- Title
- c++ transform
- Category
- C++
- Title
- how to include seld declared header file in c++
- Category
- C++
- Title
- what is difference between ciel and floor
- Category
- C++
- Title
- how to load from files C++
- Category
- C++
- Title
- c++ replace substrings
- Category
- C++
- Title
- string input
- Category
- C++
- Title
- how to convert int to string c++
- Category
- C++
- Title
- c++ code for polynomial addition
- Category
- C++
- Title
- arrays in C++
- Category
- C++
- Title
- c++ char to int
- Category
- C++
- Title
- namespace file linking c++
- Category
- C++
- Title
- find in vector in c++
- Category
- C++
- Title
- how to modulo 10^9+7
- Category
- C++
- Title
- how to find the size of a character array in c++
- Category
- C++
- Title
- clear console c++
- Category
- C++
- Title
- built in function in c++ for binary to decimal
- Category
- C++
- Title
- c++ not greater than
- Category
- C++
- Title
- coronavirus
- Category
- C++
- Title
- set lower bound c++
- Category
- C++
- Title
- convert int to string c++
- Category
- C++
- Title
- phph date
- Category
- C++
- Title
- maximum possible number atmost k swaps
- Category
- C++
- Title
- c++ switch
- Category
- C++
- Title
- error: invalid conversion from 'Node*' to 'int'
- Category
- C++
- Title
- c++ declare variable
- Category
- C++
- Title
- max element in array c++ stl
- Category
- C++
- Title
- remove element by index from vector c++
- Category
- C++
- Title
- c++ random
- Category
- C++
- Title
- c++ sort vector of objects by property
- Category
- C++
- Title
- cin.ignore
- Category
- C++
- Title
- is x prime?
- Category
- C++
- Title
- worker class c++
- Category
- C++
- Title
- array search c++
- Category
- C++
- Title
- lisy stl C++
- Category
- C++
- Title
- can we compare a long long int with int in c++ using max or min functions
- Category
- C++
- Title
- char* to int in cpp
- Category
- C++