3

I have an array, I need to return new array with the values in range (from a to b) BUT! I want to return without duplicates. I wrote the script below but it doesn't work properly.

let arr = [3, 9, 10, 23, 100, 100, 23, 4, 10, 13, 13];
let newArr = [];
let funcFilter = function(arr, a, b) {
  for(let i = 0; i< arr.length; i++) {
  if(arr[i] >= a && arr[i] <= b ) {
  if(arr.indexOf(arr[i]) !== -1)
   newArr.push(arr[i]);
}
}
return newArr;
}
console.log(funcFilter(arr, 3, 20))
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
Oksana Shukh
  • 237
  • 3
  • 12

6 Answers6

5

Ypu need to check value in newArr,so arr.indexOf(arr[i]) !== -1 needs to be change to newArr.indexOf(arr[i]) == -1

let arr = [3, 9, 10, 23, 100, 100, 23, 4, 10, 13, 13];
let newArr = [];
let funcFilter = function(arr, a, b) {
  for(let i = 0; i< arr.length; i++) {
    if(arr[i] >= a && arr[i] <= b ) {
       if(newArr.indexOf(arr[i]) == -1)
         newArr.push(arr[i]);
       }
   }
return newArr;
}
console.log(funcFilter(arr, 3, 20))
flyingfox
  • 13,414
  • 3
  • 24
  • 39
3

You can use Array.filter to filter the values that match the criteria. Then convert into Set for unique values and finally use spread syntax to convert it back into array.

let arr = [3, 9, 10, 23, 100, 100, 23, 4, 10, 13, 13];
let funcFilter = function(arr, a, b) {
    return [...new Set(arr.filter(v => v >= a && v <= b))];
}
console.log(funcFilter(arr, 3, 20))
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
2

You can use Set Object to get unique values and use filter() to filter array items;

let arr = [3, 9, 10, 23, 100, 100, 23, 4, 10, 13, 13];

let funcFilter = (arr, start, end) => Array.from(new Set(arr))
                                           .filter((v) => (v >= start && v <=end));

console.log(funcFilter(arr, 3, 20));
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
1

Try with Set because

Set object lets you store unique values of any type, whether primitive values or object references.

let arr = [3, 9, 10, 23, 100, 100, 23, 4, 10, 13, 13];
let newArr = [];
let funcFilter = function(arr, a, b) {
  for(let i = 0; i< arr.length; i++) {
  if(arr[i] >= a && arr[i] <= b ) {
  if(arr.indexOf(arr[i]) !== -1)
   newArr.push(arr[i]);
}
}

return [...new Set(newArr)];
}
console.log(funcFilter(arr, 3, 20))
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
1

You could just test agains the actual index for exluding more of the same value

if (arr.indexOf(arr[i]) === i) {
//                          ^

BTW, you could move the declaration of newArr inside of the function.

var arr = [3, 9, 10, 23, 100, 100, 23, 4, 10, 13, 13],
    funcFilter = function(arr, a, b) {
        var newArr = [];
        for (let i = 0; i < arr.length; i++) {
            if (arr[i] >= a && arr[i] <= b) {
                if (arr.indexOf(arr[i]) === i) { // take only the first found item
                    newArr.push(arr[i]);
                }
            }
        }
        return newArr;
    };
console.log(funcFilter(arr, 3, 20))
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

let arr = [3, 9, 10, 23, 100, 100, 23, 4, 10, 13, 13];
const uniqueValue = Array.from(new Set(arr));

function getValueInTherange(array, a, b) {
  return array.filter(value => value >= a && value <= b - 1);
}

console.log(getValueInTherange(uniqueValue, 10, 50));
Mohammed Ashfaq
  • 3,353
  • 2
  • 15
  • 22