code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

Repeatingelement

import java.util.Arrays;

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

class RepeatingElement {

  static int func(int[] nums) {
    boolean visited[] = new boolean[nums.length];
    for(int i = 0; i < nums.length; i++){
      if(visited[nums[i]]){
        return nums[i];
      }
      visited[nums[i]] = true;
    }
    System.out.println(Arrays.toString(visited));
    return -1;
  }

  public static void main(String[] args){
    int[] nums = new int[] {0, 2, 1, 3, 2, 2};
    int result = func(nums);
    System.out.println(result);
  }
}

Created 2021-12-26T02:36:36+00:00 · Edit