1

Consider the array [1,2,2]

The array contains two unique values: 1, 2

The array contains duplicate values: 2

The lonely integer is 1

How can the lonely integer be returned?

MikeySherm
  • 327
  • 4
  • 9

8 Answers8

6

For an array where you only care about grabbing the first integer which is lonely, you can check if the indexOf and lastIndexOf are the same. If they are, then it's lonely.

const array = [2, 2, 1, 3, 4, 3, 4];

const findLonely = (arr) => {
    for (const num of arr) {
        if (arr.indexOf(num) === arr.lastIndexOf(num)) return num;
    }
    return 'No lonely integers.';
};

console.log(findLonely(array));

If you have an array that has multiple lonely values, you can use this method to find all of the lonely values:

const array = [2, 2, 1, 3, 4, 3, 4, 6, 8, 8, 9];

const findAllLonely = (arr) => {
    const map = {};

    arr.forEach((num) => {
        // Keep track of the number of time each number appears in the array
        if (!map[num]) return (map[num] = 1);
        map[num]++;
    });

    // Filter through and only keep the values that have 1 instance
    return Object.keys(map).filter((key) => {
        return map[key] === 1;
    });
};

console.log(findAllLonely(array)); // expect [1, 6, 9]
mstephen19
  • 1,733
  • 1
  • 5
  • 20
6

const array = [0,1,2,2,1,5,4,3,4,3,2];

let lonely = array.filter((item,index)=> array.indexOf(item) === array.lastIndexOf(item));
console.log(lonely);
Tridib Panda
  • 61
  • 1
  • 2
4

Working Demo :

// Array with duplicates
const arrWithDuplicates = [1, 2, 2];
 
var result = arrWithDuplicates.sort().filter((x,i,arr) => x !== arr[i+1] && x !== arr[i-1]);
console.log(result); // [1]
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
  • It is working fine for `[0, 0, 1, 2, 1]` as well. Here is the working fiddle : https://jsfiddle.net/8Lnbvu90/ – Debug Diva May 23 '22 at 08:01
2

For each element your can use .filter() to help count the how many times the element is repeated. Then use .filter() again to return only those elements that appear once.

const nums = [1,2,2,3,4,4,4,5,5,6,7,7,7,8,8];

const singles = nums.filter(
    //count how many times each element appears
    num => nums.filter(n => n === num)
    //return only those with freq. of 1
    .length === 1
);

console.log( singles );
//OUTPUT: [  1,  3,  6 ]
PeterKA
  • 24,158
  • 5
  • 26
  • 48
0

Use a 'for' loop with the filter function to loop through the array and only return the value that appears once

const arr = [0, 1, 2, 2, 1];
let unique;

for(var i = 0; i < arr.length; i++) {
    if(a.filter(x => x == arr[i]).length == 1) {
        unique = arr[i]
    }
}

return unique;
0

O(n) complexity

function lonelyinteger(a) {
        for (let i = 0; i < a.length; i++) {
            const count = a.filter((v) => v === a[i]).length;
            if (count === 1) {
                console.log(a[i]);
                return a[i];
            }
        }
    }
vignesh
  • 2,356
  • 1
  • 14
  • 21
0

the code below solves the challenge with O(n) complexity

 function lonelyinteger(a) {
    
    let result;
    a.every((e)=>{
        if(a.filter(x=>x==e).length==1) {
            result = e;
            return false;
        }return true;
    })
    return result;
}
Mhd Wael Jazmati
  • 636
  • 9
  • 18
  • 1
    Code is a lot more helpful when it is accompanied by an explanation. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please [edit] your answer and explain how it answers the specific question being asked. See [answer]. – ChrisGPT was on strike Jun 17 '22 at 11:59
-1

If there is multiple unique number in an array = [1,2,3,4,5,3,2,1] here 4 and 5 both are unique ,there is two lonely integer so the output should be like this result = [4,5]. In case of single unique integer we can return the result as result = [3] or result = 3. The below code snippet will solve both the scenario.

const array = [1,2,3,4,5,3,2,1]

let result = []

array.every(e => {
  if(array.filter(x => x == e).length == 1) {
 result.push(e)
} 
return true
})

console.log(result)

Explanation: step by step

  1. Your desire array from where you need to get the lonely integer.

  2. We defined result as an array.

  3. You can use simple for loop or array forEach (learn about forEach).

  4. We are using array filter method (learn about filter) to get our work done. Here array.filter(x => x == e) this will result when the value of e is 1 (first element of the array) then the output will be [1,1]. So for 1 the .length == 1 will return false. This process will continue to get false and the if condition will not get executed until a the 'e' became 4 (4th element of the main array).

  5. When 'e' became 4 then the result of array.filter(x => x == 4) will be [4] so the condition array.filter(x => x == e).length == 1 will be true and the if condition will execute. And inside that we are pushing the value 4 to the result array. You can add a next line return false to stop the execution and you will get only one single lonely integer.

  6. return true is required here only if you're using the every method (learn about array every method)

Play with the code to get better understanding or comment if you've some question about this solution. Please give a up-vote if this answer is helpful.

TripleM
  • 1,096
  • 7
  • 20