数组中的计数元素只出现一次并且没有连续的下一个和上一个出现
给定一个数组arr[] 。任务是计算数组中仅出现一次的元素,并且数组中不存在连续的 next (arr[i]+1)和 prev (arr[I] – 1)元素。
例子
Input: arr[] = {7, 3, 1, 4, 5, 3, 4}
Output: 2
Explanation: Numbers 7 and 1 follows the given condition. Hence the answer is 2.
Input: arr = {10, 6, 5, 8}
Output: 2
方法:这个问题可以通过使用HashMaps来解决。将每个数字的频率存储在 hashmap 中,然后遍历数组以检查当前元素的唯一性以及其连续的下一个和前一个元素的出现。
下面是上述方法的实现。
C++
// C++ program for above approach
#include
using namespace std;
// Function to count the number of elements
// that follows the given conditions
int count(vector& nums)
{
// Take the size of array
int n = nums.size();
// Hashmap to store the frequency
// of all the elements
map m;
for (int i = 0; i < n; i++) {
if (m.find(nums[i]) == m.end())
m.insert({ nums[i], 1 });
else
m[nums[i]]++;
}
int c = 0;
for (auto i : m) {
int p = i.first;
if (m.find(p + 1) == m.end()
&& m.find(p - 1) == m.end()
&& i.second == 1)
c++;
}
return c;
}
// Driver Code
int main()
{
vector arr = { 10, 6, 5, 8 };
// Function Call
cout << count(arr);
return 0;
}
Java
// JAVA program for above approach
import java.util.*;
class GFG
{
// Function to count the number of elements
// that follows the given conditions
public static int count(ArrayList nums)
{
// Take the size of array
int n = nums.size();
// Hashmap to store the frequency
// of all the elements
Map m
= new HashMap();
for (int i = 0; i < n; i++) {
if (m.containsKey(nums.get(i)) == false)
m.put(nums.get(i), 1);
else
m.put(nums.get(i), m.get(nums.get(i) + 1));
}
int c = 0;
for (Map.Entry i : m.entrySet()) {
int p = i.getKey();
if (m.containsKey(p + 1) == false
&& m.containsKey(p - 1) == false
&& i.getValue() == 1)
c++;
}
return c;
}
// Driver Code
public static void main(String[] args)
{
ArrayList arr
= new ArrayList<>(Arrays.asList(10, 6, 5, 8));
// Function Call
System.out.print(count(arr));
}
}
// This code is contributed by Taranpreet
Python3
# Python program for above approach
# Function to count the number of elements
# that follows the given conditions
def count(nums):
# Take the size of array
n = len(nums)
# Hashmap/dictionary to store the frequency
# of all the elements
m = {}
for i in range(0, n):
if nums[i] not in m:
m[nums[i]] = 1
else:
m[nums[i]] += 1
c = 0
for i in m:
p = i
if (not m.__contains__(p+1)
and not m.__contains__(p-1)
and m[i] == 1):
c += 1
return c
# Driver Code
arr = [10, 6, 5, 8]
# Function Call
print(count(arr))
# This code is contributed by Palak Gupta
C#
// C# program for above approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
// Function to count the number of elements
// that follows the given conditions
static int count(ArrayList nums)
{
// Take the size of array
int n = nums.Count;
// Hashmap to store the frequency
// of all the elements
Dictionary m = new Dictionary();
for (int i = 0; i < n; i++) {
if (m.ContainsKey((int)nums[i]) == false)
m.Add((int)nums[i], 1);
else
m[(int)nums[i]] = m[(int)nums[i]] + 1;
}
int c = 0;
foreach(KeyValuePair i in m)
{
int p = i.Key;
if (m.ContainsKey(p + 1) == false
&& m.ContainsKey(p - 1) == false
&& i.Value == 1) {
c++;
}
}
return c;
}
// Driver Code
public static void Main()
{
ArrayList arr = new ArrayList();
arr.Add(10);
arr.Add(6);
arr.Add(5);
arr.Add(8);
// Function Call
Console.Write(count(arr));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
2
时间复杂度: O(N)
辅助空间: O(N)