Answers for "Given an array of integers arr, write a function that returns true if and only if the number of occurrences of each value in the array is unique. using hashmap"

0

Given an array of integers arr, write a function that returns true if and only if the number of occurrences of each value in the array is unique. using hashmap

import java.util.*;
class Solution {
    public boolean uniqueOccurrences(int[] arr) {
        HashMap<Integer,Integer> hash = new HashMap<>();
        for(int i = 0;i<arr.length;i++){
            if(hash.containsKey(arr[i])){
                hash.put(arr[i],hash.get(arr[i])+1);
            }else{
                hash.put(arr[i],1);
            }
        }
        for(int i = 0;i<arr.length;i++){
            for(int j = i+1;j<arr.length-1;j++){
                if(arr[i]!=arr[j] && hash.get(arr[i])==hash.get(arr[j])){
                    return false;
                }
            }
        }
      return true;
    }
}
Posted by: Guest on August-20-2020

Code answers related to "Given an array of integers arr, write a function that returns true if and only if the number of occurrences of each value in the array is unique. using hashmap"

Browse Popular Code Answers by Language