Reputation: 81
I am aware that similar questions to this have been asked, but I have looked through these and am absolutly baffled as to a) how they work, and b) how I could adapt them to fit my purpose. I have therefore started a new question.
I have an array of only 4 indices, each index holds a number. My aim is to find the lowest value in this array and return the index of that lowest value. This has not been a problem...
The problem arises when the lowest value is repeated in more than one index.
In this case what I would like is to be able to first run a "count" on the array to find out if the lowest value is repeated, then if the count is >1 then do the following: find the indices of the repeated values, Finally I need to take the values of these indices and do a furthur calculation before finding the lowest value between them.
Example:
array[ 12.44 , 10.33 , 17.45 , 10.33]
//First I need a count to find the number of times the lowest value (10.33) occurs
//Then I would like a function to return either a string containing 1,3 to
//represent the indices, or an array[ 1 , 3
Once again I apologise if this question has been answered already but please could you explain the answer as I have tried repeatedly to understand he previous answers and can't make head way.
Why is it so complicated to find repeated values in an array using js?
Thanks in advance for your help, and time!
John
Upvotes: 1
Views: 4103
Reputation: 12390
How about this way with pure JS?
var myArr = [12.44 , 10.33 , 17.45 , 10.33]; //Your array
var lowest = Math.min.apply(Math, myArr); //Find the lowest number
var count = 0; //Set a count variable
var indexes = []; //New array to store indexes of lowest number
for(var i=0; i<myArr.length;i++) //Loop over your array
{
if(myArr[i] == lowest) //If the value is equal to the lowest number
{
indexes.push(i); //Push the index to your index array
count++; //Increment your counter
}
}
alert(count); //2
alert(indexes); //1, 3
and a working jsFiddle here
Upvotes: 1
Reputation: 7297
var arr = [12.44, 10.33, 17.45, 10.33],
lowest = Math.min.apply(Math, arr), //.. 10.33
index = $.map(arr, function(o,i) { if (o === lowest) return i; }), //.. [1,3]
numOfTimes = index.length; //.. 2
explanation:
Math.min
is a function. You can call any function and change the context of that function using function.call(context, param1, param2, paramEtc...)
or function.apply(context, param[])
.
Math.min
doesn't allow us to pass in an array by calling Math.min(arr)
, since it is expecting a comma separated list of parameters; that is why we have that funny syntax Math.min.apply(Math, arr)
$.map()
is just a convenient iterator, you could have used any method to get an array of indexes
Upvotes: 0
Reputation: 236022
You could just create a filter, to filter out all duplicates and after that, run some magic on a temporarily array to get the desired number. For instance
var arr = [ 12.44 , 10.33 , 17.45 , 10.33],
filtered = [ ],
lowest;
arr.forEach(function( value ) {
if( filtered.indexOf( value ) === -1 )
filtered.push( value );
});
lowest = Math.min.apply( null, filtered ); // 10.33
arr.indexOf( lowest ); // 1
Upvotes: 0