给定两个数组,任务是找到第一个数组中存在但第二个数组中不存在的数字。
例子 :
Input : a[] = {1, 2, 3, 4, 5, 10};
b[] = {2, 3, 1, 0, 5};
Output : 4 10
4 and 10 are present in first array, but
not in second array.
Input : a[] = {4, 3, 5, 9, 11};
b[] = {4, 9, 3, 11, 10};
Output : 5
方法一(简单)
天真的方法是使用两个循环并检查第二个数组中不存在的元素。
C++
// C++ simple program to
// find elements which are
// not present in second array
#include
using namespace std;
// Function for finding
// elements which are there
// in a[] but not in b[].
void findMissing(int a[], int b[],
int n, int m)
{
for (int i = 0; i < n; i++)
{
int j;
for (j = 0; j < m; j++)
if (a[i] == b[j])
break;
if (j == m)
cout << a[i] << " ";
}
}
// Driver code
int main()
{
int a[] = { 1, 2, 6, 3, 4, 5 };
int b[] = { 2, 4, 3, 1, 0 };
int n = sizeof(a) / sizeof(a[0]);
int m = sizeof(b) / sizeof(b[1]);
findMissing(a, b, n, m);
return 0;
}
Java
// Java simple program to
// find elements which are
// not present in second array
class GFG
{
// Function for finding elements
// which are there in a[] but not
// in b[].
static void findMissing(int a[], int b[],
int n, int m)
{
for (int i = 0; i < n; i++)
{
int j;
for (j = 0; j < m; j++)
if (a[i] == b[j])
break;
if (j == m)
System.out.print(a[i] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int a[] = { 1, 2, 6, 3, 4, 5 };
int b[] = { 2, 4, 3, 1, 0 };
int n = a.length;
int m = b.length;
findMissing(a, b, n, m);
}
}
// This code is contributed
// by Anant Agarwal.
Python 3
# Python 3 simple program to find elements
# which are not present in second array
# Function for finding elements which
# are there in a[] but not in b[].
def findMissing(a, b, n, m):
for i in range(n):
for j in range(m):
if (a[i] == b[j]):
break
if (j == m - 1):
print(a[i], end = " ")
# Driver code
if __name__ == "__main__":
a = [ 1, 2, 6, 3, 4, 5 ]
b = [ 2, 4, 3, 1, 0 ]
n = len(a)
m = len(b)
findMissing(a, b, n, m)
# This code is contributed
# by ChitraNayal
C#
// C# simple program to find elements
// which are not present in second array
using System;
class GFG {
// Function for finding elements
// which are there in a[] but not
// in b[].
static void findMissing(int []a, int []b,
int n, int m)
{
for (int i = 0; i < n; i++)
{
int j;
for (j = 0; j < m; j++)
if (a[i] == b[j])
break;
if (j == m)
Console.Write(a[i] + " ");
}
}
// Driver code
public static void Main()
{
int []a = {1, 2, 6, 3, 4, 5};
int []b = {2, 4, 3, 1, 0};
int n = a.Length;
int m = b.Length;
findMissing(a, b, n, m);
}
}
// This code is contributed by vt_m.
PHP
Javascript
C++
// C++ efficient program to
// find elements which are not
// present in second array
#include
using namespace std;
// Function for finding
// elements which are there
// in a[] but not in b[].
void findMissing(int a[], int b[],
int n, int m)
{
// Store all elements of
// second array in a hash table
unordered_set s;
for (int i = 0; i < m; i++)
s.insert(b[i]);
// Print all elements of
// first array that are not
// present in hash table
for (int i = 0; i < n; i++)
if (s.find(a[i]) == s.end())
cout << a[i] << " ";
}
// Driver code
int main()
{
int a[] = { 1, 2, 6, 3, 4, 5 };
int b[] = { 2, 4, 3, 1, 0 };
int n = sizeof(a) / sizeof(a[0]);
int m = sizeof(b) / sizeof(b[1]);
findMissing(a, b, n, m);
return 0;
}
Java
// Java efficient program to find elements
// which are not present in second array
import java.util.HashSet;
import java.util.Set;
public class GfG{
// Function for finding elements which
// are there in a[] but not in b[].
static void findMissing(int a[], int b[],
int n, int m)
{
// Store all elements of
// second array in a hash table
HashSet s = new HashSet<>();
for (int i = 0; i < m; i++)
s.add(b[i]);
// Print all elements of first array
// that are not present in hash table
for (int i = 0; i < n; i++)
if (!s.contains(a[i]))
System.out.print(a[i] + " ");
}
public static void main(String []args){
int a[] = { 1, 2, 6, 3, 4, 5 };
int b[] = { 2, 4, 3, 1, 0 };
int n = a.length;
int m = b.length;
findMissing(a, b, n, m);
}
}
// This code is contributed by Rituraj Jain
Python3
# Python3 efficient program to find elements
# which are not present in second array
# Function for finding elements which
# are there in a[] but not in b[].
def findMissing(a, b, n, m):
# Store all elements of second
# array in a hash table
s = dict()
for i in range(m):
s[b[i]] = 1
# Print all elements of first array
# that are not present in hash table
for i in range(n):
if a[i] not in s.keys():
print(a[i], end = " ")
# Driver code
a = [ 1, 2, 6, 3, 4, 5 ]
b = [ 2, 4, 3, 1, 0 ]
n = len(a)
m = len(b)
findMissing(a, b, n, m)
# This code is contributed by mohit kumar
C#
// C# efficient program to find elements
// which are not present in second array
using System;
using System.Collections.Generic;
class GfG
{
// Function for finding elements which
// are there in a[] but not in b[].
static void findMissing(int []a, int []b,
int n, int m)
{
// Store all elements of
// second array in a hash table
HashSet s = new HashSet();
for (int i = 0; i < m; i++)
s.Add(b[i]);
// Print all elements of first array
// that are not present in hash table
for (int i = 0; i < n; i++)
if (!s.Contains(a[i]))
Console.Write(a[i] + " ");
}
// Driver code
public static void Main(String []args)
{
int []a = { 1, 2, 6, 3, 4, 5 };
int []b = { 2, 4, 3, 1, 0 };
int n = a.Length;
int m = b.Length;
findMissing(a, b, n, m);
}
}
/* This code contributed by PrinciRaj1992 */
Javascript
输出 :
6 5
方法二(使用哈希)
在这个方法中,我们将第二个数组的所有元素存储在一个哈希表(unordered_set)中。逐个检查第一个数组的所有元素并打印所有不存在于哈希表中的元素。
C++
// C++ efficient program to
// find elements which are not
// present in second array
#include
using namespace std;
// Function for finding
// elements which are there
// in a[] but not in b[].
void findMissing(int a[], int b[],
int n, int m)
{
// Store all elements of
// second array in a hash table
unordered_set s;
for (int i = 0; i < m; i++)
s.insert(b[i]);
// Print all elements of
// first array that are not
// present in hash table
for (int i = 0; i < n; i++)
if (s.find(a[i]) == s.end())
cout << a[i] << " ";
}
// Driver code
int main()
{
int a[] = { 1, 2, 6, 3, 4, 5 };
int b[] = { 2, 4, 3, 1, 0 };
int n = sizeof(a) / sizeof(a[0]);
int m = sizeof(b) / sizeof(b[1]);
findMissing(a, b, n, m);
return 0;
}
Java
// Java efficient program to find elements
// which are not present in second array
import java.util.HashSet;
import java.util.Set;
public class GfG{
// Function for finding elements which
// are there in a[] but not in b[].
static void findMissing(int a[], int b[],
int n, int m)
{
// Store all elements of
// second array in a hash table
HashSet s = new HashSet<>();
for (int i = 0; i < m; i++)
s.add(b[i]);
// Print all elements of first array
// that are not present in hash table
for (int i = 0; i < n; i++)
if (!s.contains(a[i]))
System.out.print(a[i] + " ");
}
public static void main(String []args){
int a[] = { 1, 2, 6, 3, 4, 5 };
int b[] = { 2, 4, 3, 1, 0 };
int n = a.length;
int m = b.length;
findMissing(a, b, n, m);
}
}
// This code is contributed by Rituraj Jain
蟒蛇3
# Python3 efficient program to find elements
# which are not present in second array
# Function for finding elements which
# are there in a[] but not in b[].
def findMissing(a, b, n, m):
# Store all elements of second
# array in a hash table
s = dict()
for i in range(m):
s[b[i]] = 1
# Print all elements of first array
# that are not present in hash table
for i in range(n):
if a[i] not in s.keys():
print(a[i], end = " ")
# Driver code
a = [ 1, 2, 6, 3, 4, 5 ]
b = [ 2, 4, 3, 1, 0 ]
n = len(a)
m = len(b)
findMissing(a, b, n, m)
# This code is contributed by mohit kumar
C#
// C# efficient program to find elements
// which are not present in second array
using System;
using System.Collections.Generic;
class GfG
{
// Function for finding elements which
// are there in a[] but not in b[].
static void findMissing(int []a, int []b,
int n, int m)
{
// Store all elements of
// second array in a hash table
HashSet s = new HashSet();
for (int i = 0; i < m; i++)
s.Add(b[i]);
// Print all elements of first array
// that are not present in hash table
for (int i = 0; i < n; i++)
if (!s.Contains(a[i]))
Console.Write(a[i] + " ");
}
// Driver code
public static void Main(String []args)
{
int []a = { 1, 2, 6, 3, 4, 5 };
int []b = { 2, 4, 3, 1, 0 };
int n = a.Length;
int m = b.Length;
findMissing(a, b, n, m);
}
}
/* This code contributed by PrinciRaj1992 */
Javascript
输出 :
6 5
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。