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

Add a main so that you can put some code there to call your functions and make s

ID: 3882696 • Letter: A

Question

Add a main so that you can put some code there to call your functions and make sure they work:

public static void main(String[] args) { /* your testing code goes here */ }

Functions you should add if the functionality is possible. Otherwise add an empty function definition with a comment saying "impossible".

Problem #9

public static boolean checkUniqueFast(int[] values) {

/* This function should return true if all the values in the array are unique - i.e. there are no duplicates. Optimize this solution for time and for large-ish arrays */

}

** Test input: empty array; {2, 4, 6, 1, 3, 5}; {2, 4, 6, 2, 4, 6}

Explanation / Answer

/* package whatever; // don't place package name! */

import java.util.*;

import java.lang.*;

import java.io.*;

import java.util.Scanner;

/* Name of the class has to be "Main" only if the class is public. */

class Ideone

{

public static void main (String[] args) throws java.lang.Exception

{

int n;

Scanner sc=new Scanner(System.in);

//System.out.println("Enter the array size");

n=sc.nextInt();

if(n<=0)

{

empty();

}

else

{

int i;

int[] values = new int[n];

// inserting array elements

for(i=0;i<n;i++)

values[i] = sc.nextInt();

boolean ans;

ans = checkUniqueFast(values);

if(ans == true)

System.out.println("True");

else

System.out.println("False");

}

}

public static boolean checkUniqueFast(int[] values)

{

boolean ans = true;

// set stores the array element

Set<Integer> a = new HashSet<Integer>();

int i,len = values.length;

for(i=0;i<len;i++)

{

if(a.contains(values[i])) // check whether the element is present in the set

{

ans = false;

return ans;

}

else

{

a.add(values[i]);

}

}

return ans;

}

public static void empty()

{

System.out.println("Impossible");

}

}

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