code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

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