0

I have a collection of data here, you can see below. What I want is to get the index of that element that has a unique value in array.

var setArray = [ false, true, false, false ]
// Sample result will be 1, the index of unique value in array is true
// and has a index of 1
// code here to get the index...

How can I solve this?

3
  • Are the values only ever going to be true or false? Commented Oct 5, 2017 at 14:38
  • 1
    please add some more use cases and what you have tried. Commented Oct 5, 2017 at 14:39
  • Hey John, check my answer, should be exactly what you are looking for. :) Commented Oct 5, 2017 at 14:52

4 Answers 4

1
var setArray = [ false, true, false, false ]

function singles( array ) {
        for( var index = 0, single = []; index < array.length; index++ ) {
            if( array.indexOf( array[index], array.indexOf( array[index] ) + 1 ) == -1 ) single.push( index );    
        };
        return single;
    };

singles(setArray); //This will return 1

A slightly modified function by ThinkingStiff on this question to suit your needs. Just pass in your array, and it'll return the index value of the unique element! That simple. Let me know how it goes.

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

Comments

1

Have you tried the following algorithm: for every item in the array find the index of first occurence and the index of the next occurence. If the index of next occurence is -1, then it is unique.

var setArray = [ false, true, false, false ];
var unique = [];

setArray.forEach(item => {
    let firstIndex = setArray.indexOf(item, 0);
  let secondIndex = setArray.indexOf(item, firstIndex + 1);
  if(secondIndex < 0) {
    unique.push(firstIndex);
  }
});

See following fiddle for example:
https://jsfiddle.net/yt24ocbs/

Comments

0

This code will return an array of indexes of all unique elements in array. It accepts different types of values: string, number, boolean.

"use strict";

let strings = ["date1", "date", false, "name", "sa", "sa", "date1", 5, "8-()"];
let result = [];

let data = strings.reduce((acc, el) => {
    acc[el] = (acc[el] || 0) + 1;
    return acc;
}, {});

let keys = Object.keys(data);

for (let i = 0, max = keys.length; i < max; i++) {
    if (data[keys[i]] === 1) {
        let index = strings.indexOf(keys[i]);

        if (index === -1) {
            index = strings.indexOf(+keys[i]);
        }

        if (index === -1) {
            index = strings.indexOf(keys[i] === true);
        }

        result.push(index);
    }
}

result.sort( (a, b) => {return a - b});

console.log(result);

Comments

0

You could map the indices for unique values and then filter just the index.

var array = [false, true, false, false],
    result = array
        .map(function (a, i, aa) { return aa.indexOf(a) === aa.lastIndexOf(a) ? i : -1; })
        .filter(function (a) { return ~a; });

console.log(result[0]);

ES6

var array = [false, true, false, false],
    result = array
        .map((a, i, aa) => aa.indexOf(a) === aa.lastIndexOf(a) ? i : -1)
        .filter(a => ~a);

console.log(result[0]);

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.