703044. The problem of identical arrays

Basic

The Problem of Identical Arrays

Slug: the-problem-of-identical-arrays3229 Difficulty: Basic Id: 703044 Topic Tags: Arrays, Sorting, Binary Search, Data Structures, Algorithms Company Tags: None

Summary

Identical arrays is a problem that requires us to find all pairs of identical arrays in an array of arrays. This problem can be solved by using binary search and sorting algorithms.

Detailed Explanation

The input array contains subarrays, which are also sorted. We need to write a function that takes this array as input and returns all the duplicate subarrays (i.e., subarrays that appear more than once in the input).

To solve this problem, we can use a two-pointer technique. First, we sort the main array. Then, we initialize two pointers, one at the start of the array and one at the end. We compare the elements at these positions. If they are equal, we know that all subarrays between these two points will also be equal (since the array is sorted), so we move both pointers towards each other until they meet. This way, we can find duplicate subarrays.

Here's a step-by-step breakdown of the solution:

  1. Sort the main array.
  2. Initialize two pointers, one at the start and one at the end of the array.
  3. Compare the elements at these positions.
  4. If they are equal, move both pointers towards each other until they meet.
  5. If all subarrays between these two points are equal, we have found a duplicate subarray.

Time complexity: O(n log n) due to sorting Space complexity: O(1) since we only use a constant amount of space

Optimized Solutions

Java

View on GeeksforGeeks
public class Main {
    public static List<List<Integer>> findDuplicates(int[][] arr) {
        List<List<Integer>> duplicates = new ArrayList<>();
        if (arr.length == 0) return duplicates;
        
        Arrays.sort(arr, (a, b) -> a.hashCode() - b.hashCode());
        
        for (int i = 1; i < arr.length; i++) {
            if (Arrays.equals(arr[0], arr[i])) {
                duplicates.add(Arrays.asList(arr[0]));
            } else {
                break;
            }
        }
        
        return duplicates;
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.