Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA ONLY && include psuedocode Write a method that checks whether two arrays ha

ID: 3710974 • Letter: J

Question

JAVA ONLY && include psuedocode

Write a method that checks whether two arrays have the same elements in some order, ignoring duplicates (the arrays are the same set). For example, the two arrays

                   1 4 9 16 9 7 4 9 11

and

                    11 11 7 9 16 4 1

would be considered the same set because their elements (ignoring duplicates) form the same set. You should include a print statement that states whether or not elements of the arrays form the same set or do not form the same set. You should also code one or more helper methods (not counting a printArray method). Think through carefully how to use step-wise refinement for this problem. Each of your methods should be short and should solve a single problem, making it reusable for you and others. NOTE: Only use methods we have already covered this semester!

In your main method, include at least 2 test cases, which means 4 different arrays, including one test that results in the arrays forming the same set and another that results in the arrays forming a different set. Use these as test cases in your program:

array1: {1,4,9,16,9,7,4,9,11} compared to array2: {11, 11, 7, 9, 16, 4, 1}

array3: {1,2,3,4,5,7} compared to array4: {5,1,4,3,2,7,7,9}

Explanation / Answer

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication23;
import java.util.Scanner;

public class sameset {
public static void insertionsort(int[] a, int low, int high) {

for (int i = low+1; i <= high; ++i) {

int temp = a[i], j = i-1; // Save the current element

while (j >= low && a[j] > temp) { // Shift all elements greater than it to the right

a[j+1] = a[j];

--j;

}

a[j+1] = temp; // Insert the current element into the correct spot

}
}
public static int removeDupicate(int[] a)
{
int len=a.length,i,j,k,count=0;
for(i=0;i<len-1-count;i++)
if(a[i]==a[i+1])
{
for(j=i;j<len-1-count;j++)
a[j]=a[j+1];
i--;
count++;
}
return (a.length-count);
}
public static boolean checkValues(int[] a,int b[],int l)
{
int i=0;
while((a[i]==b[i])&&(i<l))
i++;
if(i==l)
return true;
else return false;
}
  
public static void main(String[] args)
{
int a[],b[],c[],d[],i,n1,n2,n3,n4;
System.out.println("Enter the No.Element in Array:");
Scanner sc=new Scanner(System.in);
n1=sc.nextInt();
a= new int[n1];
//inputarray(a,n1);
System.out.println("Enter the Element in Array:");
i=0;
while(i<n1)
{
a[i]=sc.nextInt();
i++;
}
System.out.println("Enter the No.Element in Array:");
n2=sc.nextInt();
b= new int[n2];
//inputarray(b,n2);
System.out.println("Enter the Element in Array:");
i=0;
while(i<n2)
{
b[i]=sc.nextInt();
i++;
}
System.out.println("Enter the No.Element in Array:");
n3=sc.nextInt();
c= new int[n3];
i=0;
System.out.println("Enter the Element in Array:");
while(i<n3)
{
c[i]=sc.nextInt();
i++;
}
System.out.println("Enter the No.Element in Array:");
n4=sc.nextInt();
d=new int[n4];
System.out.println("Enter the Element in Array:");
i=0;
while(i<n4)
{
d[i]=sc.nextInt();
i++;
}
  
insertionsort(a,0,n1-1);
//for(i=0;i<a.length;i++)
// System.out.print(" "+a[i]);
insertionsort(b,0,n2-1);
insertionsort(c,0,n3-1);
insertionsort(d,0,n4-1);
//System.out.print(" Same Set");
n1=removeDupicate(a);
n2=removeDupicate(b);
n3=removeDupicate(c);
n4=removeDupicate(d);
//System.out.print(" Same Set");
System.out.print(" "+n1+"="+n2);
if((n1==n2)&&checkValues(a,b,n1))
System.out.print(" Same Set");
else
System.out.print(" not in same Set");
if((n3==n4)&&checkValues(c,d,n4))
System.out.print(" Same Set");
else
System.out.print(" not in same Set");
sc.close();

}
  
}

Output:

run:
Enter the No.Element in Array:
9
Enter the Element in Array:
1 4 9 16 9 7 4 9 11
Enter the No.Element in Array:
7
Enter the Element in Array:
11 11 7 9 16 4 1
Enter the No.Element in Array:
6
Enter the Element in Array:
1 2 3 4 5 7
Enter the No.Element in Array:
8
Enter the Element in Array:
5 1 4 3 2 7 7 9

6=6
Same Set
not in same SetBUILD SUCCESSFUL (total time: 1 minute 27 seconds)