Java Code 1 _ To Find the unique elements in an array
Program:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int i,j,n,count;
int freq[]=new int[100];
// Get size of the array
System.out.println("Enter size of the array:");
n=s.nextInt();
int arr[]=new int[n];
// Enter array elements
System.out.println("Enter the array elements:");
for(i=0;i<n;i++)
{
arr[i]=s.nextInt();
freq[i]=-1;
}
// Find frequency of each element in the array
for(i=0;i<n;i++)
{
count=1;
for(j=i+1;j<n;j++)
{
if(arr[i]==arr[j])
{
count++;
freq[j]=0;
}
}
if(freq[i]!=0)
{
freq[i]=count;
}
}
// Print the unique elements
System.out.println("Unique elements in the array are:");
for(i=0;i<n;i++)
{
if(freq[i]==1)
{
System.out.println(arr[i]+" ");
}
}
}
}
Output:
Thank You ...
********** The above program is contributed by Madhaneeswaran P *************
0 comments: