Home > Software engineering >  I need to write code that will add non-repeating elements to a new array from an old one
I need to write code that will add non-repeating elements to a new array from an old one

Time:12-14

I need to write code that will add non-repeating elements to a new array from an old one.

I made 2 functions similar to .includes and .push., .includes checks if the new array has the same element as the old one, if not, it returns true.

Then I turn to the .push function and add this element to the new array. The problem is that in the arrIncludesTest function loop, i is reset every time it is called and the function returns true all the time. Ready-made methods do not suit me, I have to write them myself.

function unique(arr) {
    let result = [];
    for (let elem of arr) {
        if (arrIncludesTest(result, elem)) {
            arrPush1(result, elem);
        }
    }
    return result;
}

function arrIncludesTest(result, elem) {
    for (let i = 0; i <= result.length; i  ) {
      if (result[i] !== elem) {
        return true;
      }
    }
    return false;
}

function arrPush1(result, elem){
    result[result.length] = elem
}
console.log(unique([1,2,2,3,4,4,4,44]))

CodePudding user response:

Your for loop should be using < instead of <=, otherwise i will equal the length of your array which is an invalid index. Your if-statement within arrIncludesTest should only return true when the current element equals the search elem, so you need to change it to use == (or ===), not !==. By using !== you'll almost immediately stop your loop as you'll return false because the first element of result doesn't equal your elem. Then you can push your element into your array if the current element isn't in your array yet !arrIncludesTest(result, elem).

See example below:

function unique(arr) {
  let result = [];
  for (let elem of arr) {
    if (!arrIncludesTest(result, elem)) {
      arrPush1(result, elem);
    }
  }
  return result;
}

function arrIncludesTest(result, elem) {
  for (let i = 0; i < result.length; i  ) {
    if (result[i] === elem) {
      return true;
    }
  }
  return false;
}

function arrPush1(result, elem) {
  result[result.length] = elem;
}
console.log(unique([1, 2, 2, 3, 4, 4, 4, 44]));

Your restrictions about what you can and can't use is a little vague, but I doubt that you can use a Set. As an alternative, another option could be to create an object. Object can only store unique keys, so adding the same key twice doesn't double it up. Within the object, you can add your elements as keys to the object, and then loop over the keys to get unique values:

function unique(arr) {
  const obj = {};
  for(const elem of arr)
    obj[elem] = true;
  
  const res = [];
  for(const key in obj)
    res[res.length] =  key; // convert the key to a number with the unary plus operator
  return res;
}
console.log(unique([1, 2, 2, 3, 4, 4, 4, 44]))

Although, if you're going to do something like this, a Set is more appropriate (as above we're storing an object with meaningless values)

  • Related