tarmogoyf

WHAT_IS_O(N)

Mar 21st, 2024
997
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.54 KB | None | 0 0
  1. //given : generate N unique integers, return array
  2. // N - any positive int 32
  3.  
  4. public int [] randomize(int n){
  5.     Set<Integer> memo  = new HashSet<>();
  6.     int [] result = new int[n];
  7.     for(int i = 0; i < n; i++){
  8.     int candidate = ThreadLocalRandom.current().nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE); // random int 32 from java library
  9.     while(memo.contains(candidate)){
  10.         candidate = ThreadLocalRandom.current().nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE); //
  11.     }
  12.     memo.add(candidate);
  13.     reuslt[i] = candidate;
  14. }
  15.  
  16. return result;
  17.  
  18. }
Advertisement
Add Comment
Please, Sign In to add comment