unique array js set code example
Example 1: javascript get distinct values from array
const categories = ['General', 'Exotic', 'Extreme', 'Extreme', 'General' ,'Water', 'Extreme']
.filter((value, index, categoryArray) => categoryArray.indexOf(value) === index);
This will return an array that has the unique category names
['General', 'Exotic', 'Extreme', 'Water']
Example 2: how to create an array with unique values
import java.util.Random;
public class GenRandArray {
static Random rand = new Random();
public static void fillInt( int[] fillArray ) {
rand.setSeed(System.currentTimeMillis());
for(int i = 0; i < fillArray.length; i++)
{
fillArray[i]= rand.nextInt();
for (int j = 0; j < i; j++)
{
if (fillArray[i] == fillArray[j])
{
i--;
}
}
}
}
}