Quick actions

cmd+k|ctrl+k

Navigation

Languages

HashSet - Duplicate subarray

Snippet info

Language

Csharp

Visibility

public

Author

takutapfu

Created

2025-10-08T13:59:34.057694Z

Updated

2025-10-08T13:59:34.057694Z

using System;
using System.Collections.Generic;
using System.Linq;

//Write a function that determines if an array contains any duplicate subarray of size k.
//Use a hash-based approach to achieve O(n) average complexity.

class MainClass {
    static void Main() {
        int[] nums = { 1, 2, 3, 1, 2, 3, 4 };
        int k = 3;
        
        bool result = ProcessDuplicates(nums, k);
        Console.WriteLine(result);
    }
    
    public static bool ProcessDuplicates(int[] nums, int k){
        HashSet<int> set = new HashSet<int>();
        foreach(var num in nums){
            set.Add(num);
        }
        
        if((nums.Length - set.Count) == k){
            return true;
            }
        return false;
    }
}
INFO