0

I have a question. I'm looking for a way to get the higest unique number of an array.

var temp = [1, 8, 8, 8, 4, 2, 7, 7];

Now I want to get the output 4 since that is the unique highest number.

Is there a good & hopefully short way to do that?

Tushar
  • 85,780
  • 21
  • 159
  • 179
Romano
  • 117
  • 1
  • 2
  • 12

3 Answers3

2

Yes, there is:

Math.max(...temp.filter(el => temp.indexOf(el) == temp.lastIndexOf(el)))

Explanation:

  1. First, get the elements which are unique in the array using Array#filter

    temp.filter(el => temp.indexOf(el) === temp.lastIndexOf(el)) // [1, 4, 2]
    
  2. Now, get the max of the numbers from the array using ES6 spread operator

    Math.max(...array) // 4
    

    This code is equivalent to

    Math.max.apply(Math, array);
    
Tushar
  • 85,780
  • 21
  • 159
  • 179
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    I don't think arrow functions have wide support across browsers yet. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions – NickT Mar 28 '16 at 14:26
  • @NickT: Probably more than spread arguments… But it doesn't matter, OP tagged his question ES6 so I used it. – Bergi Mar 28 '16 at 14:27
  • 1
    Am I mussing something. Because I get NaN.... I did ---> console.log(Math.max(temp.filter(el => temp.indexOf(el) == temp.lastIndexOf(el)))); – Romano Mar 28 '16 at 14:33
  • @Romano: You're missing the `...`. See [Demo here](http://babeljs.io/repl/#?evaluate=true&presets=es2015&experimental=false&loose=false&spec=false&code=var%20temp%20%3D%20%5B1%2C%208%2C%208%2C%208%2C%204%2C%202%2C%207%2C%207%5D%3B%0D%0Aconsole.log(Math.max(...temp.filter((el%2C%20i)%20%3D%3E%20temp.indexOf(el)%20%3D%3D%20temp.lastIndexOf(el))))) – Bergi Mar 28 '16 at 14:34
1

If you don't want to get fancy, you can use a sort and loop to check the minimal number of items:

var max = 0;
var reject = 0;

// sort the array in ascending order
temp.sort(function(a,b){return a-b});
for (var i = temp.length - 1; i > 0; i--) {
  // find the largest one without a duplicate by iterating backwards
  if (temp[i-1] == temp[i] || temp[i] == reject){
     reject = temp[i];
     console.log(reject+" ");
  }
  else {
     max = temp[i];
     break;
  }

}
NickT
  • 214
  • 1
  • 5
0

Using the spread operator you can find the hightest number easily

Math.max(...numArray);

The only thing left then is to either filter duplicates from the array beforehand, or remove all the elements that match your maximum number if its a duplicate.

remove beforeHand would be easiest in es6 like this.

Math.max(...numArray.filter(function(value){ return numArray.indexOf(value) === numArray.lastIndexOf(numArray);}));

For a non es6 compatible way to remove duplicates have a look at Remove Duplicates from JavaScript Array, the second answer contains an extensive examinations of several alternatives

Community
  • 1
  • 1
Louis
  • 593
  • 4
  • 13
  • 2
    `Set` doesn't actually remove all elements that are non-unique, it only removes their duplicates so that one of each stays in the set. I think OP is expecting the result 4, not 8. – Bergi Mar 28 '16 at 14:37