# 1507. Dot Product of Two Sparse Vectors ###### tags: `Leetcode` `FaceBook` `Medium` Link: https://leetcode.com/problems/dot-product-of-two-sparse-vectors/ ## Code ```java= class SparseVector { Map<Integer, Integer> mapping; SparseVector(int[] nums) { mapping = new HashMap<Integer, Integer>(); for(int i = 0;i < nums.length;i++){ if(nums[i]!=0){ mapping.put(i,nums[i]); } } } // Return the dotProduct of two sparse vectors public int dotProduct(SparseVector vec) { int result = 0; for(int i:this.mapping.keySet()){ if(vec.mapping.containsKey(i)){ result+=mapping.get(i)*vec.mapping.get(i); } } return result; } } ```