skip to Main Content

would appreciate if anyone could tell me why this isn’t working?

function(array){

var uniqueArray = [];
for (i=0 ; i < array.length; i++) {
for (j=0; j < uniqueArray.length; j++){
  if (array[i]===uniqueArray[j]) {
    } else {
      uniqueArray.push(array[i]);
    }
  }
}
return uniqueArray;
}

I’ve seen something online about using two more variables:The count and start variables
But I don’t understand why those are necessary. As far as I can tell, mine does the same thing? Nothing if i=j and .push if it doesn’t.

3

Answers


  1. Your code can’t work because, as noticed by @pilchard, the inner loop will never run: uniqueArray has a starting legth of 0 so it is never cycled ad no values are added to it.
    Furthermore, the if condition of the inner loop is also wrong: assuming that the array contains values, every time a non-matching value is found the tested value is appended.

    This is a possible solution keeping the code similar to the one you proposed:

    function arrayUnique (array){
    
        let uniqueArray = [];
        let i, j;
    
        for (i=0 ; i < array.length; i++) {
    
            for (j=0; j < uniqueArray.length; j++) {
    
               if (array[i]===uniqueArray[j]) {
                      break;  // ok, the value is already there, let's stop
               }
            }
            
            // if j exceeds the uniqueArray last element index we have not found the searched value...
            if (j == uniqueArray.length) 
              uniqueArray.push(array[i]); // ...so we can add it
    
        }
    
       return uniqueArray;
    }
    
    const testArray1 = [1, 2, 3, 1, 1, 2, 4, 1];
    console.log (arrayUnique(testArray1));
    
    const testArray2 = ["a", "aa", "b", "aa", "c", "b"];
    console.log (arrayUnique(testArray2));
    
    
    Login or Signup to reply.
  2. Instead of using the inner loop, you can use the array built-in method includes to check whether the element is already in the array or not.

    function unique(array) {
    
        var uniqueArray = [];
    
        for (i=0 ; i < array.length; i++) {
            // if not already in the array, add that to array
            if (! uniqueArray.includes(array[i]) ) {
              uniqueArray.push(array[i]);
            }
        }
    
        return uniqueArray;
    }
    
    // sample run
    console.log(unique([1,2,3,2,2]))
    console.log(unique([1,1,1,1,1]))
    console.log(unique([1,2,3,2,1]))
    console.log(unique([]))

    P.S

    A better approach would be to use the set (because includes will work fast in hash set).

    Login or Signup to reply.
  3. Regarding your code you try to add to the unique array while looping it which never happens initially. So you should add to the unique array outside the check loop.

    But by my benchmarks using Set is the fastest way to get a unique array in JS. Using an array is the slowest – o(n^2).

    Using Array’s built-in methods like Array::reduce() and Array::includes() instead of manual looping gives more decent results.

    Btw, I find that using a block label is elegant in that case and isn’t the label anti-pattern since the label’s scope is very local.

    enter image description here

    <script benchmark data-count="1">
    
        const count = 100000;
        const arr = Array.from({ length: count }, () => Math.random());
    
        function getUniqueArray(arr) {
            return [...new Set(arr)];
        }
    
        function getUniqueArray2(arr) {
    
            const out = [];
            const set = {};
    
            for (let i = 0; i < arr.length; i++) {
                const val = arr[i];
                if (val in set) {
                    continue;
                }
                out.push(val);
            }
            return out;
        }
    
        function getUniqueArray3(arr) {
    
            const out = [];
    
            input: for (let i = 0; i < arr.length; i++) {
                for (let j = 0; j < out.length; j++) {
                    if (arr[i] === out[j]) {
                        continue input;
                    }
                }
                out.push(arr[i]);
            }
    
            return out;
        }
    
        function getUniqueArray4(arr) {
    
            return arr.reduce((unique, item) => {
                unique.includes(item) || unique.push(item);
                return unique;
            }, []);
    
        }
    
        // @benchmark using Set
        getUniqueArray(arr);
    
        // @benchmark using object
        getUniqueArray2(arr);
    
        // @benchmark using array looping with 1/2 data
        getUniqueArray3(arr.slice(0, arr.length / 2));
    
        // @benchmark using array methods with 1/2 data
        getUniqueArray4(arr.slice(0, arr.length / 2));
    
    </script>
    <script src="https://cdn.jsdelivr.net/gh/silentmantra/benchmark/loader.js"></script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search