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

Distinctelements

import java.util.HashSet;

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

class DistinctElements {

  static int func(int[] nums){
    HashSet<Integer> map = new HashSet<Integer>();
    for(int i = 0; i < nums.length; i++){
      map.add(nums[i]);
    }
    return map.size();
  }

  public static void main(String[] args){
    int arr[] = new int[]{15, 16, 27, 27, 28, 15};
    int result = func(arr);
    System.out.println(result);
  }
}

Created 2021-12-26T06:33:28+00:00 · Edit