1

Input: array=[2,5,1,2,3,5,1,2,4]

Output return 2

Input: array=[2,1,1,2,3,5,1,2,4]

Output return 1

Input array=[2,3,4,5]

Output return undefined

I run my code on repl.it and it always return undefined.

Is there any improve ways to let undefined disappear?

Also how to return undefined in third input?

What i try in my JS:

   
function FirstRecurringCharacter(arr){
for (i = 0; i < arr.length; i++) {
  if (arr.indexOf(arr[i]) !== i) {
    console.log(arr[i]);
    break;
  }
}
};
   
 FirstRecurringCharacter([2,5,1,2,3,5,1,2,4])//should return 2
   
 FirstRecurringCharacter([2,1,1,2,3,5,1,2,4])//should return 1
   
 FirstRecurringCharacter([2,3,4,5])//should rerturn undefined

1
  • 6
    When you want to return something, you should use a return statement. Commented Mar 4, 2019 at 13:22

6 Answers 6

5

You could take a Set and use Array#find.

function find(array) {
    var s = new Set;
    return array.find(v => s.has(v) || !s.add(v));
}

console.log(find([2, 5, 1, 2, 3, 5, 1, 2, 4])); // 2
console.log(find([2, 1, 1, 2, 3, 5, 1, 2, 4])); // 1
console.log(find([2, 3, 4, 5]));                // undefined

Sign up to request clarification or add additional context in comments.

5 Comments

OK..What are .add and .has mean?
If set already has the element then add else return ans because now you got same element again
add is to add a value to the set and has test the set, if the value is in the set.
why the !s.add instead of just s.add? trying to wrap my brain around that...
@ToddAustin s.add(v) is to add the value to the set. this returns always the instance of the used set. the second expression is now always truthy, but not wanted, because only the first part should decide and if a value is not in the set, it should return a falsy value.
3

You should return the value, not log to console.

   
function FirstRecurringCharacter(arr){
  for (i = 0; i < arr.length; i++) {
    if (arr.indexOf(arr[i]) !== i) {
      return arr[i];
    }
  }
  return undefined;
};
   
 console.log(FirstRecurringCharacter([2,5,1,2,3,5,1,2,4]))//should return 2
   
 console.log(FirstRecurringCharacter([2,1,1,2,3,5,1,2,4]))//should return 1
   
 console.log(FirstRecurringCharacter([2,3,4,5]))//should rerturn undefined

2 Comments

I only modified your code, @Nina Scholz's answer is better, you should refer to it.
Ari Can you please explain why there is no arr.indexOf(arr[i]) > -1 in the condition?
0
function firstReccuringChar(arr) {
  let map = {};
  for (let i = 0; i < arr.length; i++) {
    if (map[arr[i]] !== undefined) {
      return arr[i];
    } else {
      map[arr[i]] = i;
    }
    console.log(map);
  }
  return undefined;
}

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

// Another example using Hash Tables or Objects.

1 Comment

Hey, thanks for answering! Your answer will be more helpful if you described the code that you have written.
0
function firstRecurringCharacter(input) {
  let result = {};
  let len = input.length;

  for (let i = 0; i < len; i++) {
    if (result[input[i]] !== undefined) {
      return input[i];
    } else {
      result[input[i]] = i;
    }
  }

  return undefined;
}

3 Comments

This is the correct answer to the problem. As stated, in the exact same answer above. This is obviously a question in a classroom or online course regarding hash tables. It should have been obvious by the posters question.
I don't understand your comment. You said it's the exact same answer as another answer. Which one, and why post a new answer if they're the same? It would help for you to include some explanation as to how this solves the problem in ways that haven't been posted already
needed answer with explanation
0
const firstReccuringChar = (arr) => {
    for (let i = 0; i < arr.length; i++) {
       for (let j = i + 1; j < array.length; j++) {
          if (arr[i] === arr[j]) {
            return arr[i];
         }
        }
     }
  return undefined;
  };

this is another solution but it's not the best one because it's O(n^2)

Comments

0

Since you want to log to console and also seems like you are attempting a naive solution, I presented my solution in the same manner. Time Complexity is O(n^2).

function firstRecurringCharacter(input) {
  let index = Infinity;
  let firstRecurringValue = 0;
  for (let i = 0; i < input.length; i++) {
    for (let j = i + 1; j < input.length; j++) {
      if (input[i] === input[j]) {
        // Edge Case: Selects lowest index repeated while traversing through the nested loop
        if (j < index) {
          index = j;
          firstRecurringValue = input[j];
        }
      }
    }
  }
  return firstRecurringValue !== 0
    ? console.log(firstRecurringValue)
    : console.log(undefined);
}
firstRecurringCharacter([2, 5, 1, 2, 3, 5, 1, 2, 4]); // returns 2
firstRecurringCharacter([2, 1, 1, 2, 3, 5, 1, 2, 4]); // returns 1
firstRecurringCharacter([2, 3, 4, 5]); // returns undefined

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.