1var arr = [55, 44, 65,1,2,3,3,34,5];
2var unique = [...new Set(arr)]
3
4//just var unique = new Set(arr) wont be an array
1import java.util.Random;
2
3public class GenRandArray {
4 //Recommend reseeding within methods
5 static Random rand = new Random();
6
7 public static void fillInt( int[] fillArray ) {
8 //seeds the random for a new random each method call
9 rand.setSeed(System.currentTimeMillis());
10
11 for(int i = 0; i < fillArray.length; i++)
12 {
13 fillArray[i]= rand.nextInt();
14
15 /* for loop to check to make sure that none of the previous
16 numbers added are the same as the one just added. */
17 for (int j = 0; j < i; j++)
18 {
19 /* if so we need to redo the last random number by
20 moving i back one index to be re-set */
21 if (fillArray[i] == fillArray[j])
22 {
23 i--;
24 }
25 }
26 }
27 }
28}
1import java.util.ArrayList;
2
3public class UniqueNumbersInArray {
4
5 public static void main(String[] args) {
6
7
8 int arr[] = { 4,5,5,4,5,6,5,8,4,7};
9
10 ArrayList<Integer> uniqueArr = new ArrayList<Integer>();
11 for (int i = 0; i < arr.length; i++) {
12
13 int k = 0;
14 if (!uniqueArr.contains(arr[i])){
15 uniqueArr.add(arr[i]);
16 k++;
17
18 for (int j = i; j < args.length ; j++) {
19 if (arr[i]==arr[j]){
20 k++;
21 }
22 }
23
24 System.out.println(arr[i]);
25 System.out.println(k);
26 }
27
28 }
29 }
30}