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

Problem 1. Write a method that takes an array as its parameter and returns the s

ID: 3574216 • Letter: P

Question

 Problem 1.           Write a method that takes an array as its parameter and returns the sorted array.            If there are duplicate elements then you should remove duplicates. It means all            elements in result should be unique.                  Case example:                 Array                           Result                 [2,6,10,3]                      [2,3,6,10]                 [2,3,2,3,2]                     [2,3]                 [3,2,7,7,1]                     [1,2,3,7]                 [1,1,1]                         [1]                 []                                      []         */         public int[] UniqueSortedArray(int[] arr) {                 // TODO                 return arr;         }                  /*         Problem 2.           Write a method that takes an array as its parameter and returns the largest distance            between two elements that have the same value. An element has a distance of zero from            itself, and to adjacent elements with the same value hava a distance of one.            If no two elements have the same value then you return a largest distance of 0.                  Case examples:                 Array                           Result                 [2,3,6,10]                      0                 [0,0,0,0,0]                     4                 [2,3,2,3,2]                     4                 [3,2,7,7,1]                     1                 [4,5,100,4,5,100]       3                 [1]                                     0                 []                                      0         */         public int CalLargestDistance(int[] arr) {                 // TODO                 return 0;         }                           //====================================================================================         // YOU DON'T NEED TO MODIFY BELOW THIS LINE.         //====================================================================================         public void printArray(int[] arr) {                 System.out.print("[");                 for (int i = 0; i < arr.length; i++) {                         System.out.print(arr[i]);                         if (i != arr.length - 1) System.out.print(", ");                 }                 System.out.println("]");         }                  public static void main(String[] args) {                 Lab2 l = new Lab2();                                  System.out.println("===== Problem 1 =====");                 int[][] arr1 = {{2,6,10,3},{2,3,2,3,2},{3,2,7,7,1},{1,1,1},{}};                 for (int i = 0; i < arr1.length; i++) {                         l.printArray(arr1[i]);                         System.out.print("-> ");                         l.printArray(l.UniqueSortedArray(arr1[i]));                 }                                  int[][] arr2 = {{2,3,6,10},{0,0,0,0,0},{2,3,2,3,2},{3,2,7,7,1},{4,5,100,4,5,100},{1},{}};                 System.out.println(" ===== Problem 2 =====");                 for (int i = 0; i < arr2.length; i++) {                         l.printArray(arr2[i]);                         System.out.println("-> " + l.CalLargestDistance(arr2[i]));                 }         } } 

Explanation / Answer

package snippet;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;

class Lab2 {

  
   public int[] UniqueSortedArray(int[] a)
   {
       for (int i = 0; i < a.length; i++)
   {
   for (int j = i + 1; j < a.length; j++)
   {
   if (a[i] > a[j])
   {
   int temp = a[i];
   a[i] = a[j];
   a[j] = temp;
   }
   }
   }
      
      
      
       int j = 0;
int i = 1;
if(a.length < 2){
return a;
}
while(i < a.length){
if(a[i] == a[j]){
i++;
}else{
a[++j] = a[i++];
}   
}
int[] output = new int[j+1];
for(int k=0; k<output.length; k++){
output[k] = a[k];
}
return output;

   }
  
  
   private int CalLargestDistance(int[] a) {
       // TODO Auto-generated method stub
       int maxd=0;
      
       for(int i=0;i<a.length;i++)
       {
           for(int j=i+1;j<a.length;j++)
           {
               if(a[i]==a[j])
               {
                   int diff=i-j;
                   if(Math.abs(diff)>maxd)
                   {
                       maxd=Math.abs(diff);
                   }
                      
               }
           }
       }
       return maxd ;
   }
   public void printArray(int[] arr) {
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
if (i != arr.length - 1) System.out.print(", ");
}
System.out.println("]");
}
  
public static void main(String[] args) {
Lab2 l = new Lab2();
  
System.out.println("===== Problem 1 =====");
int[][] arr1 = {{2,6,10,3},{2,3,2,3,2},{3,2,7,7,1},{1,1,1},{}};
for (int i = 0; i < arr1.length; i++) {
l.printArray(arr1[i]);
System.out.print("-> ");
l.printArray(l.UniqueSortedArray(arr1[i]));
}
  
int[][] arr2 = {{2,3,6,10},{0,0,0,0,0},{2,3,2,3,2},{3,2,7,7,1},{4,5,100,4,5,100},{1},{}};
System.out.println(" ===== Problem 2 =====");
for (int i = 0; i < arr2.length; i++) {
l.printArray(arr2[i]);
System.out.println("-> " + l.CalLargestDistance(arr2[i]));
}
}

}

====================================================================================

Output:

OpenJDK 64-Bit Server VM warning: Insufficient space for shared memory file:
7762
Try using the -Djava.io.tmpdir= option to select an alternate temp location.

===== Problem 1 =====
[2, 6, 10, 3]
-> [2, 3, 6, 10]
[2, 3, 2, 3, 2]
-> [2, 3]
[3, 2, 7, 7, 1]
-> [1, 2, 3, 7]
[1, 1, 1]
-> [1]
[]
-> []

===== Problem 2 =====
[2, 3, 6, 10]
-> 0
[0, 0, 0, 0, 0]
-> 4
[2, 3, 2, 3, 2]
-> 4
[3, 2, 7, 7, 1]
-> 1
[4, 5, 100, 4, 5, 100]
-> 3
[1]
-> 0
[]
-> 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote