2

When I have a duplicate on first part of the element I want to return the largest. I have sorted the array and it looks like:

var sortedMyArray = ["4ff02829-df7e/0", "4ff02829-df7e/10", "8c138c02-c139/37", "54be465f-0c03/41", "b2036e03-384f/4", "b2036e03-384f/32", "b2036e03-384f/35"];

My expected result is:

["4ff02829-df7e/10", "8c138c02-c139/37", "54be465f-0c03/41", "b2036e03-384f/35"];

My code so far is:

for(var i=0;i<sortedMyArray.length;i++){
                    for (var k = i + 1; k < sortedMyArray.length; k++) {
                     if(sortedMyArray[i].split('/')[0] == sortedMyArray[k].split('/')[0]){
                          console.log('First values of element equal so remove the smallest element');

                            if(sortedMyArray[i].split('/')[1]<sortedMyArray[k].split('/')[1]){
                                sortedMyArray.splice(sortedMyArray[i],1);
                            }
                        }
                     }
                  }

3 Answers 3

3

Since you're array is sorted, you can just filter out each element that has a next element with the same string up to the '/':

result = sortedMyArray.filter( function ( el, i ) {
    if ( i + 1 === sortedMyArray.length )
        return true;
    return el.split( '/' )[ 0 ] !== sortedMyArray[ i + 1 ].split( '/' )[ 0 ];
} );

This assumes that your array is sorted correctly, so you always want to keep the last entry with each prefix. Just in case, I suggest that you double check that your sorting behaves the way you want for cases ending in /2 and /10.

Sign up to request clarification or add additional context in comments.

1 Comment

"b2036e03-384f/4" comes before "b2036e03-384f/32" so the sorting algorithm looks to be correct.
1

This uses Array.reduce() to put the first part of each element and the full element into an object as key/value pairs. When there's duplicate keys it compares the value after the slash to store the larger value. Then returns all the values in an array.

var sortedMyArray = ["4ff02829-df7e/0", "4ff02829-df7e/10", "8c138c02-c139/37", "54be465f-0c03/41", "b2036e03-384f/4", "b2036e03-384f/32", "b2036e03-384f/35"];

var reduced = Object.values(sortedMyArray.reduce((acc, val) => {
  const [ preSlash, postSlash ] = val.split('/');

  if (acc[preSlash]) {
    const stored = acc[preSlash].slice(acc[preSlash].indexOf('/'));
    return postSlash > stored
        ? Object.assign(acc, { [preSlash]: val })
      : acc;
  } else {
    return Object.assign(acc, { [preSlash]: val });
  }
}, {}));

console.log(Object.values(reduced));

https://jsfiddle.net/g2rkmn9m/

Comments

1

Use Array#reduce to iterate the array with an object as the accumulator.

On each iteration split the element by the delimiter, and use the first element of the result as a property in order to either initialize it or update it to the current element.

Finally use Object.values to get an array of the unique elements.


Benchmarking shows this to be faster than both of the other methods at the time of posting.

var sorted = ["4ff02829-df7e/0", "4ff02829-df7e/10", "8c138c02-c139/37", "54be465f-0c03/41", "b2036e03-384f/4", "b2036e03-384f/32", "b2036e03-384f/35"];
var unique = sorted.reduce((u, e) => ((u[e.split('/')[0]] = e), u), {});
console.log(Object.values(unique));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.