Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get index of sorted list where element changes?

I have the following array and I am looking to retrieve the index of the original (sorted) array where the element is changing and how often that individual element exists.

ab = [1,1,1,3,3,5,5,5,5,5,6,6]

The desired outcome should be like this:

ac = [0,3,5,10]
ad = [3,2,5,2]

Thank you very much for any suggestion.

Cheers.

like image 232
AppRoyale Avatar asked Mar 07 '19 17:03

AppRoyale


People also ask

How do I find the index of a specific element?

To find the index of an element in a list, you use the index() function. It returns 3 as expected.

How to find the index of an element in a list?

Find the index of an element in a list using the index() method We can use the index() method to find the first occurrence of an element in a list. The index() method takes the element as the first input argument which is compulsory. It takes two optional arguments which are indices at which search has to start and stop in the list.

How do you find the index of a sorted array?

In an array which is sorted in increasing order all the elements before val are less than val. So, to get the indices of val in a sorted array, instead of performing sort operation, we can simply count the elements less than val. If the count is x then val will start from x-th index (because of 0-based indexing).

How to access a SortedSet element by Index?

As noted by @DavidRR, you could use the Linq extension method Enumerable.ElementAt (). However, since the backing store of a SortedSet is a red-black tree -- a height-balanced binary tree, accessing an element by index via ElementAt () involves a tree walk — O (N), worst case and O (N/2) on the average, to get to the desired item.

How to find all occurrences of an element in a list?

List is: [1, 2, 3, 4, 5, 6, 7, 8, 2, 3, 46, 67, 23] Number is: 100 100 is not in the list. We can find the first index of an element in a list using the index() method but we cannot use it to find all the occurrences of any element in the list. We will use for loop to iterate the list to find all the occurrences of any element in the list.


4 Answers

You could iterate the array and check the predecessor. If equal, increment the last count, otherwise add the index and a count of one.

var array = [1, 1, 1, 3, 3, 5, 5, 5, 5, 5, 6, 6],
    { indices, counts } = array.reduce((r, v, i, a) => {
        if (a[i - 1] === v) {
            r.counts[r.counts.length - 1]++;
        } else {
            r.indices.push(i);
            r.counts.push(1);
        }
        return r;
    }, { indices: [], counts: [] });

console.log(...indices);
console.log(...counts);
like image 111
Nina Scholz Avatar answered Oct 23 '22 14:10

Nina Scholz


This code produces similar output to the one you posted:

var ab = [1,1,1,3,3,5,5,5,5,5,6,6];

var ac = Array.from(new Set(ab.map((e) => ab.indexOf(e))));

var ad = [];

for (var i = 0; i < ac.length - 1; i++) {
  ad.push(ac[i + 1] - ac[i]);
}
ad.push(ab.length - ac[ac.length - 1]);

console.log(...ab);
console.log(...ac);
console.log(...ad);
like image 26
MrGeek Avatar answered Oct 23 '22 16:10

MrGeek


Try this, should get you what you want

        ab = [1,1,1,3,3,5,5,5,5,5,6,6];

        var items = [];
        var positions = [];
        var count = [];

        ab.map((item, index)=>{

            //check if exist
            let item_index = items.indexOf(item);
            if(item_index == -1) {
                items.push(item);
                positions.push(index);
                count.push(1);
            } else {
                let current_count = count[item_index];
                count[item_index] = ++current_count;
            }
        });

        console.log(positions);
        console.log(count);
like image 38
Jonathan K Avatar answered Oct 23 '22 15:10

Jonathan K


so, using https://underscorejs.org/#groupBy you can group by value

_.groupBy([1,1,1,3,3,5,5,5,5,5,6,6]);

or 

_.groupBy([1,1,1,3,3,5,5,5,5,5,6,6], function(num){ return num; })

you will get an object like

{1: [1,1,1], 3: [3,3], 5: [5,5,5,5,5], 6: [6,6]}

so if you take all https://underscorejs.org/#keys and iterate through, value under key is array, take size and append to new array, so you can make ad = [3,2,5,2]

again, iterate through keys and get https://underscorejs.org/#indexOf , you can construct ac = [0,3,5,10]

play around these methods, check examples, and you can do it yourself!

like image 38
Dmitri Algazin Avatar answered Oct 23 '22 15:10

Dmitri Algazin