shiva reddy
shiva reddy

Reputation: 31

how to print a unique number in a array

The problem is to find the unique number in a array such as [2,2,2,5]. The output should be 5 as it is the 1 unique element in the array.

I have attempted this:

function findUniq(arr) {

    var b= arr[0];
    var c;
    for(var i=0; i<arr.length; i++)
    { 

      if(arr[i]===b )
      {
         b=arr[i]

         }
     else

       {
       c=arr[i];
  }
}
return c
console.log(findUniq([3, 5, 3, 3, 3]))

This works fine unless the unique number is the first element in the array. How do I fix this?

Upvotes: 2

Views: 318

Answers (5)

Jee Mok
Jee Mok

Reputation: 6556

if you prefer .reduce over .map for your use case (for performance/etc. reasons):

function existance(data) {
   return data.reduce((a, c) => (data.indexOf(c) === data.lastIndexOf(c)) ? a.concat(c) : a, []);
}

console.log(existance([1,1,1,2]));
console.log(existance([1,1,2,3,4,5,5,6,6,6]));

Upvotes: 0

L&#233;a Gris
L&#233;a Gris

Reputation: 19555

This is another implementation that is surely less efficient than that of @Nick's, but it is a valid algorithm anyway:

function findUniq(arr) {

  var elemCount = new Map();
  var uniq = [];

  // Initialize elements conts
  for (var k of arr.values()) {
    elemCount.set(k, 0);
  }

  // Count elements
  for (var k of arr.values()) {
    elemCount.set(k, elemCount.get(k) + 1);
  }

  // Add uniq elements to array
  for (var [k, v] of elemCount.entries()) {
    if (v === 1) uniq.push(k);
  }

  return uniq;
}
console.log(findUniq([3, 5, 3, 3, 3]))

Upvotes: 0

user8866053
user8866053

Reputation:

Try something like this using a set, which only stores unique elements:

var set = new Set(arr);
// count instances of each element in set
result = {};
for(var i = 0; i < a.length; ++i) {
    if(!result[arr[i]])
        result[arr[i]] = 0;
    ++result[arr[i]];
}
for (var value in result) {
    if (value == 1) {
        return value;
    }
}
// if there isn't any
return false;

This should work, please tell me if it doesn't.

Upvotes: 0

Taki
Taki

Reputation: 17654

You can create a recursive function that will take the first element of the array and see if it exists in the rest of it, if it does, it will take the next element and do the same, return the element if it doesn't exist in the rest of the array :

const arr = [3, 3, 3, 5, 3];

const find = arr => {
  const [f, ...rest] = arr;
  if(rest.includes(f))
    return find(rest);
  else 
   return f;
}

const result = find(arr);

console.log(result);

Note that this will return the last element if all of them are the same [3,3,3] will return 3

Upvotes: 0

Nick
Nick

Reputation: 147166

You can use indexOf and lastIndexOf to see if a value occurs more than once in the array (if it does, they will be different), and if so, it is not the unique value. Use filter to process the array:

let array = [2,2,2,5];
console.log(array.filter(v => array.indexOf(v) === array.lastIndexOf(v)));
array = [5,3,3,3,3];
console.log(array.filter(v => array.indexOf(v) === array.lastIndexOf(v)));
array = [4,4,5,4];
console.log(array.filter(v => array.indexOf(v) === array.lastIndexOf(v)));

Upvotes: 4

Related Questions