Example 1: javascript array unique values
var arr = [55, 44, 65,1,2,3,3,34,5];
var unique = [...new Set(arr)]
Example 2: javascript find unique values in array
var myArray = ['a', 1, 'a', 2, '1'];
var unique = myArray.filter((v, i, a) => a.indexOf(v) === i);
Example 3: get only unique values from array javascript
arr = [2, 2, 2, 1, 3, 3, 3,3, 4, 5];
arr = arr.sort().filter((item,i)=>!(arr[i] == arr[i+1] || arr[i-1]==arr[i]));
console.log(arr);
Example 4: 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--;
}
}
}
}
}
Example 5: array with unique values javascript
let uniqueItems = [...new Set(items)]
Example 6: unique elements in array javascript
var array3 = [1, 2, 4, 6, 1, 4, 9, 10, 2, 8];
function findUniqueElements_3(array) {
for (let i = 0; i < array.length; i++) {
for (let j = i + 1; j < array.length; j++) {
if (array[i] == array[j]) {
array.splice(j, 1)
}
}
}
console.log(`from third way : ${array}`);
}
findUniqueElements_3(array3)