检查数组中的所有重复元素是否相邻
给定一个数组arr[] 。任务是检查arr[]中的重复元素是否连续。
例子
Input: arr[] = {1, 2, 3, 4, 5, 6}
Output: Yes
Explanation: There is no duplicate element in arr[] so there is no need to check anything and answer is Yes.
Input: arr[] = {1, 2, 2, 4}
Output: Yes
Explanation: 2 is occurring 2 times and it is contiguous. Hence, answer is Yes.
Input: arr[] = {1, 2, 3, 2, 4}
Output: No
Explanation: There is a gap between 2’s and 3 is between two 2’s. Therefore, the answer is No.
方法:这个问题可以通过使用HashMaps来解决。请按照以下步骤解决给定的问题。
- 使用地图存储访问过的元素。
- 首先,在地图上标记第一个元素。
- 遍历数组arr[]从1到N-1 。其中N是arr[]的大小。
- 如果当前元素与前一个元素匹配,则意味着有一个元素重复循环,因此只需继续循环即可。
- 如果当前元素已在地图中标记,则返回“否” 。
- 标记地图中的当前元素。
- 如果函数到达这里意味着有所有连续的元素,所以返回“是” 。
下面是上述方法的实现。
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to check whether duplicate
// elements in array arr[] are contiguous or not
string checkContiguous(int* arr, int& n)
{
int i;
// Map to keep track of elements
unordered_map visited;
visited.clear();
visited.insert({ arr[0], 1 });
for (i = 1; i < n; i++) {
if (arr[i] == arr[i - 1])
continue;
else if (visited[arr[i]])
return "No";
visited[arr[i]] = 1;
}
return "Yes";
}
// Driver Code
int main()
{
int arr[] = { 2, 4, 5, 5, 3, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
cout << checkContiguous(arr, N);
return 0;
}
Java
// Java code for the above approach
import java.io.*;
class GFG {
// Function to check whether duplicate
// elements in array arr[] are contiguous or not
static String checkContiguous(int[] arr, int n)
{
int i;
// Map to keep track of elements
int[] visited = new int[n];
for (i = 1; i < n; i++) {
if (arr[i] == arr[i - 1])
continue;
else if (visited[arr[i]] == 0)
return "No";
visited[arr[i]] = 1;
}
return "Yes";
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 2, 4, 5, 5, 3, 5 };
int N = arr.length;
// Function Call
System.out.println(checkContiguous(arr, N));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python program for the above approach
# Function to check whether duplicate
# elements in array arr[] are contiguous or not
def checkContiguous (arr, n):
i = None
# Map to keep track of elements
visited = [0] * n;
for i in range(1, n):
if (arr[i] == arr[i - 1]):
continue;
elif (visited[arr[i]] == 0):
return "No";
visited[arr[i]] = 1;
return "Yes";
# Driver Code
arr = [2, 4, 5, 5, 3, 5];
N = len(arr)
# Function Call
print(checkContiguous(arr, N));
# This code is contributed by Saurabh Jaiswal
C#
// C# code for the above approach
using System;
class GFG {
// Function to check whether duplicate
// elements in array arr[] are contiguous or not
static String checkContiguous(int[] arr, int n)
{
int i;
// Map to keep track of elements
int[] visited = new int[n];
for (i = 1; i < n; i++) {
if (arr[i] == arr[i - 1])
continue;
else if (visited[arr[i]] == 0)
return "No";
visited[arr[i]] = 1;
}
return "Yes";
}
// Driver Code
public static void Main()
{
int[] arr = { 2, 4, 5, 5, 3, 5 };
int N = arr.Length;
// Function Call
Console.WriteLine(checkContiguous(arr, N));
}
}
// This code is contributed by ukasp.
Javascript
输出
No
时间复杂度: O(N)
辅助空间: O(N)