Jacky
Jacky

Reputation: 895

Find first recurring character using javascript

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

Upvotes: 1

Views: 1542

Answers (6)

EaglePrem
EaglePrem

Reputation: 21

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

Upvotes: 0

the-tunwebdev
the-tunwebdev

Reputation: 1

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)

Upvotes: 0

Elugens
Elugens

Reputation: 22

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;
}

Upvotes: 0

kulotjunior07
kulotjunior07

Reputation: 1

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.

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386720

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

Upvotes: 5

Onur Arı
Onur Arı

Reputation: 512

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

Upvotes: 3

Related Questions