Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to find the index value of the data from array who's contain 800 data approximately and it is also contain lots of same value.

What I have tried:

copyarray = yAxisdata.slice(0);  
length=yAxisdata.length;   
for (k = 0; k <length-1; k++)
{ 
     maxvalue = copyarray[k];
     indexvalue = yAxisdata.indexOf(maxvalue);
}


Above code is just an example of my long code. I am using this code to find the index of a number which is available into the array, This code is working perfectly but the problem is, if this array contain a multiple same value for example if i have 1,1 number is available in array at that time the above code is always provide an index value of first 1 and skip the second 1.
Posted
Updated 18-Jan-21 22:52pm
v2
Comments
Maciej Los 19-Jan-21 2:50am    
And you're suprised that indexOf returns the index of first instance of value in the array?
Member 14743579 19-Jan-21 3:06am    
I am trying to get the index values of all data which is available into the array including same values if you have a solution about it please tell me.
Richard MacCutchan 19-Jan-21 6:33am    
Then you will need to create a new array to hold all the index values that you find.
Richard Deeming 19-Jan-21 4:44am    
Is this C#, or is it Javascript? They are two completely different languages.

The int declarations would seem to suggest C#. But the slice call would suggest Javascript.
Member 14743579 19-Jan-21 4:51am    
This is javascript code, I Added the int for understanding. Now i am going to remove int part from my code if you confused.

1 solution

Well... below code returns the first occurance of value in the array.
JavaScript
indexvalue = yAxisdata.indexOf(maxvalue);

To get second occurance of value, use this:
JavaScript
secondindex = yAxisdata.indexOf(maxvalue, yAxisdata.indexOf(maxvalue)+1);


To get more examples, visit this thread: javascript - How to get the position of the second occurrence? - Stack Overflow[^]
 
Share this answer
 
Comments
Member 14743579 19-Jan-21 3:04am    
I know about this code but my problem is I have an arraylist which contain 800 data approximately including lots of same data, I want to get the index values of all the values which is an available into the arraylist include the same values.

Note: I don't know how many values are same in this array please give me a solution if you know.
CPallini 19-Jan-21 3:10am    
5.
Member 14743579 19-Jan-21 3:19am    
ok, but I want to find the index of every number which is available in array including same numbers.
Maciej Los 19-Jan-21 3:38am    
So, what's stoppping you? You've got an idea! Use it!
Maciej Los 19-Jan-21 3:37am    
Thank you, Carlo.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900