Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

added code for checking duplicate in a sorted array #500

Merged
merged 2 commits into from
Oct 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include<bits/stdc++.h>
using namespace std;


int main()
{
int n;
cin>>n;
//size of array

int arr[n];
//Take input of a sorted array

for(int i=0;i<n;i++)
cin>>arr[i];

int cnt=1;
// bcz arr[0] is always in output array

for(int i=1;i<n;i++)
{
// arr[i]==arr[i-1] then arr[i] is a duplicate

if(arr[i]!=arr[i-1])
{
arr[cnt]=arr[i];
cnt++;
}

}

cout<<cnt<<endl;

for(int i=0;i<cnt;i++)
{
cout<<arr[i]<<" ";
}

return 0 ;
}

/*

Sample test case:-

input:
8
-1 -1 2 3 3 3 5 7

output:
5
-1 2 3 5 7

*/