JavaScript How to return the first occurrence of repeated item in an array using for loop? return first occurrence excel,return first occurrence in sql,grep

I am trying to build logic currently with arrays and data structure. I am trying to implement the logic using for loop

function getRepeatingNumber(arr) {

  for (var i = 0; i < arr.length; i++) {
    for (var j = i + 1; j < arr.length; j++) {
      if (arr[i] === arr[j]) {
        return arr[i];
      }
    }
  }
  return undefined;
}
getRepeatingNumber([2, 3, 6, 5, 2]);

the above function takes in array and returns a repeated item in the array so in the above case it will return 2. But what if I have an array something like this arr[2,3,3,6,5,2] in this case it should return 3 but as the outer loop has index [0] which is 2 as the reference it will return 2 as the answer.

How to implement a function that returns the first occurrence of the repeated item.

Answer:1

Instead of iterating with j in the part after i, iterate the part before i:

function getRepeatingNumber(arr){
    for (var i = 1; i < arr.length; i++) {
        for (var j = 0; j < i; j++) {
            if (arr[i] === arr[j]) {
                return arr[i];
            }
        }
    }
}
console.log(getRepeatingNumber([2,3,3,6,5,2]));
Answer:2

Following is what I'm trying to do this in nodejs. The Rest API takes a city name as an input. I am trying to get the latitude and longitude using the geocode API for the input city. then, using the ...

Following is what I'm trying to do this in nodejs. The Rest API takes a city name as an input. I am trying to get the latitude and longitude using the geocode API for the input city. then, using the ...

  1. node.js avoid nested callbacks
  2. node js nested callbacks

I want to hide the images which are broken in the code below. The issue is that i not having access to the html code as it is getting generated dynamically so need to do it in JS only. I am newbee in ...

I want to hide the images which are broken in the code below. The issue is that i not having access to the html code as it is getting generated dynamically so need to do it in JS only. I am newbee in ...

  1. hide broken images
  2. hide broken images javascript
  3. hide broken images wordpress
  4. css hide broken images
  5. jquery hide broken images
  6. html hide broken images
  7. angularjs hide broken images

Let me show you an example of what I want to accomplish: Say I want to call a functions sub function's sub function's sub function (and so on, lets say for 50+ sub functions), like: foo(arg).bar(...

Let me show you an example of what I want to accomplish: Say I want to call a functions sub function's sub function's sub function (and so on, lets say for 50+ sub functions), like: foo(arg).bar(...

I have a <CountDown/> component that I'd like to hold it's own logic so it can be reusable else where in the app. I'm struggling to reason how could I setState to show:true on a sibling ...

I have a <CountDown/> component that I'd like to hold it's own logic so it can be reusable else where in the app. I'm struggling to reason how could I setState to show:true on a sibling ...

  1. react state from props
  2. react state from child to parent
  3. react state from parent
  4. react state from another component
  5. react state from child
  6. react state from function
  7. react state from localstorage
  8. react state from context
  9. react state in functional component
  10. react/state-in-constructor
  11. react state in constructor or not
  12. react/state-in-constructor eslint
  13. react state in component
  14. react state in class
  15. react state in render
  16. react state in typescript
  17. react state in class component
  18. react state in const
  19. react state in child component
  20. react state for checkbox