BackmediumArraysAmazon

Duplicate Coordinate Pairs Solution

Problem Statement

You are given an array of coordinate pairs coords, where each coordinate pair is represented as an array of two integers [x, y]. Find all pairs of indices (i, j) such that coords[i] is equal to coords[j] and i < j. Return the count of such pairs.

Example 1
Input
[[1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2]]
Output
3

Explanation: Step-by-step: with input [[1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2]], we first create a hashmap to store the frequency of each coordinate pair. Then, we iterate through the hashmap and for each pair, we calculate the number of pairs that can be formed with the current pair. Finally, we return the total count of pairs.

Example 2
Input
[[1, 2], [2, 3], [1, 2], [3, 4], [1, 2], [4, 5]]
Output
4

Explanation: Step-by-step: with input [[1, 2], [2, 3], [1, 2], [3, 4], [1, 2], [4, 5]], we first create a hashmap to store the frequency of each coordinate pair. Then, we iterate through the hashmap and for each pair, we calculate the number of pairs that can be formed with the current pair. Finally, we return the total count of pairs.

Constraints

  • {"name":"Coordinate Range","description":"-10^9 <= x, y <= 10^9"}
  • {"name":"Array Length","description":"1 <= coords.length <= 10^5"}
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

šŸš€ Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Duplicate Coordinate Pairs — Problem Statement & Solution Guide

ArraysMediumHashMap / HashSet
TimeO(n)
|
SpaceO(n)

Problem Description

You are given an array of coordinate pairs coords, where each coordinate pair is represented as an array of two integers [x, y]. Find all pairs of indices (i, j) such that coords[i] is equal to coords[j] and i < j. Return the count of such pairs.

Examples

Example 1

Input

[[1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2]]

Output

3

Explanation: Step-by-step: with input [[1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2]], we first create a hashmap to store the frequency of each coordinate pair. Then, we iterate through the hashmap and for each pair, we calculate the number of pairs that can be formed with the current pair. Finally, we return the total count of pairs.

Example 2

Input

[[1, 2], [2, 3], [1, 2], [3, 4], [1, 2], [4, 5]]

Output

4

Explanation: Step-by-step: with input [[1, 2], [2, 3], [1, 2], [3, 4], [1, 2], [4, 5]], we first create a hashmap to store the frequency of each coordinate pair. Then, we iterate through the hashmap and for each pair, we calculate the number of pairs that can be formed with the current pair. Finally, we return the total count of pairs.

Constraints

  • {"name":"Coordinate Range","description":"-10^9 <= x, y <= 10^9"}
  • {"name":"Array Length","description":"1 <= coords.length <= 10^5"}

Optimal Approach & Strategy

The optimized approach uses a HashMap to store the coordinates and their indices, allowing for a time complexity of O(n). This approach iterates over the array once, making it much more efficient for large arrays.

Brute Force Approach

The brute-force approach involves comparing each pair of coordinates in the array, resulting in a time complexity of O(n²). This approach is inefficient for large arrays.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function duplicateCoordinatePairs(coords) {
  let count = 0;
  let map = new Map();
  for (let i = 0; i < coords.length; i++) {
    let key = coords[i].join(',');
    if (map.has(key)) {
      count += map.get(key);
      map.set(key, map.get(key) + 1);
    } else {
      map.set(key, 1);
    }
  }
  return count;
}

Asked in Top Tech Interviews

Amazon

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.