I have a json array that has multiple missing numbers and is out of sequence. What is the best way to find which numbers are missing?
My First thought was to iterate through and construct a new temporary array in order (so if the first key is 50, it goes to arr[50]) and then find out which do not have a key. Unfortunately this seems extremely inefficient.
Update:
Here’s a bit of my json:
"groups": [
{
"group_id": "1",
"group_name": "AABYODAADAAAW6KAAA",
},
{
"group_id": "5",
"group_name": "AABYODAADAAAW6KAAB",
},
{
"group_id": "2",
"group_name": "AABYODAADAAAW6KAAC",
},
{
"group_id": "3",
"group_name": "AABYODAADAAAW6KAAAD",
},
{
"group_id": "6",
"group_name": "AABYODAADAAAW6KAAAE",
}
]
and I’m sorting group_id, but the array length is over 2,000.
Assuming this is a JS object you’re talking about (and not a JS Array or a JSON Array or a JSON Object), you’ll have to loop twice:
Edit: Based on your updated sample, it appears that you have an array of objects whose keys are strings that represent integers, and you want to figure out keys might be missing. Here’s code that would do that: