// to delete the duplicates in n array so that a number may appear only once in the array
import java.io.*;
class remDup
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the length of the array: ");
int l=Integer.parseInt(br. readLine());
int[]a=new int[l];
for(int i=0; i<l; i++)
{
System.out.print("Enter the element number "+(i+1)+" of the array: ");
a[i]=Integer.parseInt(br. readLine());
}
//now the real work begins
for(int i=0; i<l; i++)
{
for(int j=i+1; j<l; j++)
{
while(a[i]==a[j])
{
for(int A=j; A<l-1; A++)
a[A]=a[A+1];
l--;
}
}
}
// real work is done now!!!!
for(int i=0; i<l; i++)
{
System.out.println(a[i]);
}
}// end of main
}// end of class
class remDup
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the length of the array: ");
int l=Integer.parseInt(br.
int[]a=new int[l];
for(int i=0; i<l; i++)
{
System.out.print("Enter the element number "+(i+1)+" of the array: ");
a[i]=Integer.parseInt(br.
}
//now the real work begins
for(int i=0; i<l; i++)
{
for(int j=i+1; j<l; j++)
{
while(a[i]==a[j])
{
for(int A=j; A<l-1; A++)
a[A]=a[A+1];
l--;
}
}
}
// real work is done now!!!!
for(int i=0; i<l; i++)
{
System.out.println(a[i]);
}
}// end of main
}// end of class
i was searching for it.
ReplyDeletety
ur welcome.. :)
Deletei found ArrayIndexOutOfBoundException from above program.
ReplyDeleteplease reply me....
Can you tell me the input for which you got that error?
DeleteJust make this correction in the code:
Deletewhile(a[i]==a[j] && j<l)
And it shall work fine.
[Right now it's only "while(a[i]==a[j])" ]
This comment has been removed by the author.
ReplyDeleteint a[] = {6,6,6,3,0,2,2, 25, 35, 25}; for this getting array index out of bound.
ReplyDeleteMay i know what would be the time and space complexity of the above logic..
ReplyDeleteI think the time complexity must be of the order of n² in average cases.
DeleteThe inner most while loop may not run more than once in the best case.
I think we can safely say it's of the order of n².
I have no idea about the measures of space complexity.
can u please explain as to why are we decrementing the value of l inside the inner while loop??
ReplyDeletegot it..lite!! :)
Deletequite helpful....ty :) i used it to get a diffrent result
ReplyDelete