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

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