one way is find the frequency of each element in the array. If frequency is 1 then it is non repeated element.
Seond way is
import java.util.*;
public class Splitting
{
public static void main(String[] args)
{
int n,j,i,c=0;
boolean flag=true;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
int a[] = new int[n];
for (i = 0; i < n; i++)
{
a[i] = sc.nextInt();
}
for (i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j)
{
if(a[i]!=a[j])
{
flag=true;
}
else
{
flag=false;
break;
}
}
}
if(flag==true)
{c++;
System.out.print(a[i]+" ");
}
}
System.out.println("count is"+ c);
}
}
i/p:6
1 2 1 3 2 4
o/p:
3 4 count is2
Seond way is
import java.util.*;
public class Splitting
{
public static void main(String[] args)
{
int n,j,i,c=0;
boolean flag=true;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
int a[] = new int[n];
for (i = 0; i < n; i++)
{
a[i] = sc.nextInt();
}
for (i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j)
{
if(a[i]!=a[j])
{
flag=true;
}
else
{
flag=false;
break;
}
}
}
if(flag==true)
{c++;
System.out.print(a[i]+" ");
}
}
System.out.println("count is"+ c);
}
}
i/p:6
1 2 1 3 2 4
o/p:
3 4 count is2
No comments:
Post a Comment