This is what my program looks like so far import javautilRan

This is what my program looks like so far:

import java.util.Random;
public class Algorithm3{
   static int n = 10;
    static int k = 5;
    static Random rand = new Random();
    static int[] array = new int[n];
  
   public static void main(String[] args){
       System.out.println(\"Algorithm 3:\");
       System.out.println(\"Oct. 11, 2016\");
      
       System.out.println(\"Sanity check for n = 10 and k = 5: \");
       for(int i = 0; i <= 5; i++){
           createArray(array);
           elapsedTime();
       }
   }
  
   public static void createArray(int[] array){
       for(int i = 0; i < n; i++){
           array[i] = rand.nextInt();
       }
   }
  
   public static void uniqueNums(int[] array){
       System.out.println(\"\ The unique numbers: \");
      
        int size = array.length;
        for(int j = 0; j < k; j++){
           int index = rand.nextInt(size);
           array[index] = array[size - 1];
           size--;
           System.out.print(array[index] + \" \");
        }
   }
  
   public static long elapsedTime(){
       long startTime = System.nanoTime();
       uniqueNums(array);
       System.out.println(\"\ Execution time: \" + (System.nanoTime() - startTime));
       return System.nanoTime() - startTime;
   }
}

What I need to do is output 5 unique values in the array using the algorithm in uniqueNums and no boolean array, but I am not sure how to do that without using a boolean array.

Solution

Hi friend, you can do using HashSet .

Please look at my code:

import java.util.HashSet;

import java.util.Random;

public class Algorithm3{

   static int n = 10;

static int k = 5;

   static Random rand = new Random();

static int[] array = new int[n];

   public static void main(String[] args){

       System.out.println(\"Algorithm 3:\");

       System.out.println(\"Oct. 11, 2016\");

       System.out.println(\"Sanity check for n = 10 and k = 5: \");

       for(int i = 0; i < 5; i++){

           createArray(array);

           elapsedTime();

       }

   }

   public static void createArray(int[] array){

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

           array[i] = rand.nextInt();

       }

   }

   public static void uniqueNums(int[] array){

       System.out.println(\"\ The unique numbers: \");

       HashSet<Integer> set = new HashSet<>();

       int size = array.length;

       int count = 0;

       while(size > 0 ){

           int index = rand.nextInt(size);

           if(!set.contains(array[index])){

               System.out.print(array[index] + \" \");

               set.add(array[index]); // adding in set

              

               count++;

           }

          

           if(count == 5){

               break; // we print 5 unique numbers

           }

          

           array[index] = array[size - 1];

           size--;

          

       }

   }

   public static long elapsedTime(){

       long startTime = System.nanoTime();

       uniqueNums(array);

       System.out.println(\"\ Execution time: \" + (System.nanoTime() - startTime));

       return System.nanoTime() - startTime;

   }

}

/*

Sample Output:

Algorithm 3:

Oct. 11, 2016

Sanity check for n = 10 and k = 5:

The unique numbers:

-1614506247 1739320712 725745822 -1405745826 -216760896

Execution time: 278444

The unique numbers:

68532114 -148783137 1262674573 -717692720 -306986699

Execution time: 172150

The unique numbers:

-1031910199 574786113 -533653595 -2117828057 2033076920

Execution time: 138894

The unique numbers:

148403596 252171368 -1637673820 372532911 1420022487

Execution time: 151669

The unique numbers:

-422759789 -731748066 -1262373706 2030491604 -651788796

Execution time: 146570

*/

This is what my program looks like so far: import java.util.Random; public class Algorithm3{ static int n = 10; static int k = 5; static Random rand = new Rando
This is what my program looks like so far: import java.util.Random; public class Algorithm3{ static int n = 10; static int k = 5; static Random rand = new Rando
This is what my program looks like so far: import java.util.Random; public class Algorithm3{ static int n = 10; static int k = 5; static Random rand = new Rando
This is what my program looks like so far: import java.util.Random; public class Algorithm3{ static int n = 10; static int k = 5; static Random rand = new Rando

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site