Find the first element that occurs an even number of times in an array
Given an array which contains integer elements. Our goal is to find first element which is occurring Even Number of times. for example.
Example A
arr[] = {1,2,3,4,3,5,4}
Elements which are appear in Even number of times
3 = occurs 2
4 = occurs 2
------------------
Result : 3 [First element]
Example B
arr[] = {-1,-2,-2,-2,-3,-3,-4,-2,8}
Result : -2
First simplest solution using of two loops. This process are need O(n^2) times. See this solution.
// C Program
// Find the first element that occurs an even number of times in an array
// Time complexity O(n²)
#include <stdio.h>
void firstEvenOccurs(int arr[], int n)
{
int count = 0;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (arr[i] == arr[j])
{
if (i < j)
{
// Already check
count = 1;
// like break
j = n;
}
else
{
// Count element
count++;
}
}
}
if (count % 2 == 0)
{
// Display result
printf("\n %d", arr[i]);
return;
}
count = 0;
}
printf("\n None \n");
}
int main()
{
int arr[] = {
2 , 3 , 2 , 4 , 6 , 2 , 6 , 7 , 3 , 4 , 5 , 3
};
// Get the size of array
int n = sizeof(arr) / sizeof(arr[0]);
// Test
firstEvenOccurs(arr, n);
return 0;
}
Output
4
// Java Program
// Find the first element that occurs an even number of times in an array
// Time complexity O(n²)
public class Counting
{
public void firstEvenOccurs(int[] arr, int n)
{
int count = 0;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (arr[i] == arr[j])
{
if (i < j)
{
// Already check
count = 1;
// like break
j = n;
}
else
{
// Count element
count++;
}
}
}
if (count % 2 == 0)
{
// Display result
System.out.print("\n " + arr[i]);
return;
}
count = 0;
}
System.out.print("\n None \n");
}
public static void main(String args[])
{
Counting task = new Counting();
int[] arr = {
2 , 3 , 2 , 4 , 6 , 2 , 6 , 7 , 3 , 4 , 5 , 3
};
// Get the size of array
int n = arr.length;
// Test
// 4 Occurs 2 times
task.firstEvenOccurs(arr, n);
}
}
Output
4
// Include header file
#include <iostream>
using namespace std;
// C++ Program
// Find the first element that occurs an even number of times in an array
// Time complexity O(n²)
class Counting
{
public: void firstEvenOccurs(int arr[], int n)
{
int count = 0;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (arr[i] == arr[j])
{
if (i < j)
{
// Already check
count = 1;
// like break
j = n;
}
else
{
// Count element
count++;
}
}
}
if (count % 2 == 0)
{
// Display result
cout << "\n " << arr[i];
return;
}
count = 0;
}
cout << "\n None \n";
}
};
int main()
{
Counting *task = new Counting();
int arr[] = {
2 , 3 , 2 , 4 , 6 , 2 , 6 , 7 , 3 , 4 , 5 , 3
};
// Get the size of array
int n = sizeof(arr) / sizeof(arr[0]);
// Test
// 4 Occurs 2 times
task->firstEvenOccurs(arr, n);
return 0;
}
Output
4
package main
import "fmt"
// Go Program
// Find the first element that occurs an even number of times in an array
// Time complexity O(n²)
type Counting struct {}
func getCounting() * Counting {
var me *Counting = &Counting {}
return me
}
func(this Counting) firstEvenOccurs(arr[] int, n int) {
var count int = 0
for i := 0 ; i < n ; i++ {
for j := 0 ; j < n ; j++ {
if arr[i] == arr[j] {
if i < j {
// Already check
count = 1
// like break
j = n
} else {
// Count element
count++
}
}
}
if count % 2 == 0 {
// Display result
fmt.Print("\n ", arr[i])
return
}
count = 0
}
fmt.Print("\n None \n")
}
func main() {
var task * Counting = getCounting()
var arr = [] int {
2,
3,
2,
4,
6,
2,
6,
7,
3,
4,
5,
3,
}
// Get the size of array
var n int = len(arr)
// Test
// 4 Occurs 2 times
task.firstEvenOccurs(arr, n)
}
Output
4
// Include namespace system
using System;
// Csharp Program
// Find the first element that occurs an even number of times in an array
// Time complexity O(n²)
public class Counting
{
public void firstEvenOccurs(int[] arr, int n)
{
int count = 0;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (arr[i] == arr[j])
{
if (i < j)
{
// Already check
count = 1;
// like break
j = n;
}
else
{
// Count element
count++;
}
}
}
if (count % 2 == 0)
{
// Display result
Console.Write("\n " + arr[i]);
return;
}
count = 0;
}
Console.Write("\n None \n");
}
public static void Main(String[] args)
{
Counting task = new Counting();
int[] arr = {
2 , 3 , 2 , 4 , 6 , 2 , 6 , 7 , 3 , 4 , 5 , 3
};
// Get the size of array
int n = arr.Length;
// Test
// 4 Occurs 2 times
task.firstEvenOccurs(arr, n);
}
}
Output
4
<?php
// Php Program
// Find the first element that occurs an even number of times in an array
// Time complexity O(n²)
class Counting
{
public function firstEvenOccurs($arr, $n)
{
$count = 0;
for ($i = 0; $i < $n; ++$i)
{
for ($j = 0; $j < $n; ++$j)
{
if ($arr[$i] == $arr[$j])
{
if ($i < $j)
{
// Already check
$count = 1;
// like break
$j = $n;
}
else
{
// Count element
$count++;
}
}
}
if ($count % 2 == 0)
{
// Display result
echo("\n ".$arr[$i]);
return;
}
$count = 0;
}
echo("\n None \n");
}
}
function main()
{
$task = new Counting();
$arr = array(2, 3, 2, 4, 6, 2, 6, 7, 3, 4, 5, 3);
// Get the size of array
$n = count($arr);
// Test
// 4 Occurs 2 times
$task->firstEvenOccurs($arr, $n);
}
main();
Output
4
// Node JS Program
// Find the first element that occurs an even number of times in an array
// Time complexity O(n²)
class Counting
{
firstEvenOccurs(arr, n)
{
var count = 0;
for (var i = 0; i < n; ++i)
{
for (var j = 0; j < n; ++j)
{
if (arr[i] == arr[j])
{
if (i < j)
{
// Already check
count = 1;
// like break
j = n;
}
else
{
// Count element
count++;
}
}
}
if (count % 2 == 0)
{
// Display result
process.stdout.write("\n " + arr[i]);
return;
}
count = 0;
}
process.stdout.write("\n None \n");
}
}
function main()
{
var task = new Counting();
var arr = [2, 3, 2, 4, 6, 2, 6, 7, 3, 4, 5, 3];
// Get the size of array
var n = arr.length;
// Test
// 4 Occurs 2 times
task.firstEvenOccurs(arr, n);
}
main();
Output
4
# Python 3 Program
# Find the first element that occurs an even number of times in an array
# Time complexity O(n²)
class Counting :
def firstEvenOccurs(self, arr, n) :
count = 0
i = 0
while (i < n) :
j = 0
while (j < n) :
if (arr[i] == arr[j]) :
if (i < j) :
# Already check
count = 1
# like break
j = n
else :
# Count element
count += 1
j += 1
if (count % 2 == 0) :
# Display result
print("\n ", arr[i], end = "")
return
count = 0
i += 1
print("\n None ")
def main() :
task = Counting()
arr = [2, 3, 2, 4, 6, 2, 6, 7, 3, 4, 5, 3]
# Get the size of list
n = len(arr)
# Test
# 4 Occurs 2 times
task.firstEvenOccurs(arr, n)
if __name__ == "__main__": main()
Output
4
# Ruby Program
# Find the first element that occurs an even number of times in an array
# Time complexity O(n²)
class Counting
def firstEvenOccurs(arr, n)
count = 0
i = 0
while (i < n)
j = 0
while (j < n)
if (arr[i] == arr[j])
if (i < j)
# Already check
count = 1
# like break
j = n
else
# Count element
count += 1
end
end
j += 1
end
if (count % 2 == 0)
# Display result
print("\n ", arr[i])
return
end
count = 0
i += 1
end
print("\n None \n")
end
end
def main()
task = Counting.new()
arr = [2, 3, 2, 4, 6, 2, 6, 7, 3, 4, 5, 3]
# Get the size of array
n = arr.length
# Test
# 4 Occurs 2 times
task.firstEvenOccurs(arr, n)
end
main()
Output
4
// Scala Program
// Find the first element that occurs an even number of times in an array
// Time complexity O(n²)
class Counting()
{
def firstEvenOccurs(arr: Array[Int], n: Int): Unit = {
var count: Int = 0;
var i: Int = 0;
while (i < n)
{
var j: Int = 0;
while (j < n)
{
if (arr(i) == arr(j))
{
if (i < j)
{
// Already check
count = 1;
// like break
j = n;
}
else
{
// Count element
count += 1;
}
}
j += 1;
}
if (count % 2 == 0)
{
// Display result
print("\n " + arr(i));
return;
}
count = 0;
i += 1;
}
print("\n None \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Counting = new Counting();
var arr: Array[Int] = Array(2, 3, 2, 4, 6, 2, 6, 7, 3, 4, 5, 3);
// Get the size of array
var n: Int = arr.length;
// Test
// 4 Occurs 2 times
task.firstEvenOccurs(arr, n);
}
}
Output
4
import Foundation;
// Swift 4 Program
// Find the first element that occurs an even number of times in an array
// Time complexity O(n²)
class Counting
{
func firstEvenOccurs(_ arr: [Int], _ n: Int)
{
var count: Int = 0;
var i: Int = 0;
while (i < n)
{
var j: Int = 0;
while (j < n)
{
if (arr[i] == arr[j])
{
if (i < j)
{
// Already check
count = 1;
// like break
j = n;
}
else
{
// Count element
count += 1;
}
}
j += 1;
}
if (count % 2 == 0)
{
// Display result
print("\n ", arr[i], terminator: "");
return;
}
count = 0;
i += 1;
}
print("\n None ");
}
}
func main()
{
let task: Counting = Counting();
let arr: [Int] = [2, 3, 2, 4, 6, 2, 6, 7, 3, 4, 5, 3];
// Get the size of array
let n: Int = arr.count;
// Test
// 4 Occurs 2 times
task.firstEvenOccurs(arr, n);
}
main();
Output
4
// Kotlin Program
// Find the first element that occurs an even number of times in an array
// Time complexity O(n²)
class Counting
{
fun firstEvenOccurs(arr: Array < Int > , n: Int): Unit
{
var count: Int = 0;
var i: Int = 0;
while (i < n)
{
var j: Int = 0;
while (j < n)
{
if (arr[i] == arr[j])
{
if (i < j)
{
// Already check
count = 1;
// like break
j = n;
}
else
{
// Count element
count += 1;
}
}
j += 1;
}
if (count % 2 == 0)
{
// Display result
print("\n " + arr[i]);
return;
}
count = 0;
i += 1;
}
print("\n None \n");
}
}
fun main(args: Array < String > ): Unit
{
val task: Counting = Counting();
val arr: Array < Int > = arrayOf(2, 3, 2, 4, 6, 2, 6, 7, 3, 4, 5, 3);
// Get the size of array
val n: Int = arr.count();
// Test
// 4 Occurs 2 times
task.firstEvenOccurs(arr, n);
}
Output
4
When resultant element not exist in array in this case we display None value.
Efficient Approach Using map to count frequency of array elements and check which first element occurs Even time show this element. Using are map to count frequency of array elements. And check that which of first element are occur Even number of time.
import java.util.HashMap;
// Java program for
// Find the first element that occurs an even number of times in an array
public class Searching
{
public void firstEvenOccurs(int[] arr, int n)
{
// This record is used to collect frequency of elements
HashMap < Integer, Integer > record =
new HashMap < Integer, Integer > ();
for (int i = 0; i < n; ++i)
{
if (record.containsKey(arr[i]))
{
// Increase frequency
record.put(arr[i], record.get(arr[i]) + 1);
}
else
{
// Add new key value
record.put(arr[i], 1);
}
}
for (int i = 0; i < n; ++i)
{
if (record.get(arr[i]) % 2 == 0)
{
// Found first element which are appear odd times
System.out.println(arr[i]);
return;
}
}
System.out.println("No Result");
}
public static void main(String[] args)
{
Searching task = new Searching();
// Array of integer elements
int[] arr = {
2 , 3 , 2 , 4 , 6 , 2 , 6 , 7 , 3 , 4 , 5 , 3
};
// Get the length
int n = arr.length;
// Find resultant element
// First element which occurs Even number times
// Here 4 occurring 2 times
task.firstEvenOccurs(arr, n);
}
}
Output
4
// Include header file
#include <iostream>
#include <unordered_map>
using namespace std;
// C++ program for
// Find the first element that occurs an even number of times in an array
class Searching
{
public: void firstEvenOccurs(int arr[], int n)
{
// This record is used to collect frequency of elements
unordered_map < int, int > record;
for (int i = 0; i < n; ++i)
{
if (record.find(arr[i]) != record.end())
{
// Increase frequency
record[arr[i]] = record[arr[i]] + 1;
}
else
{
// Add new key value
record[arr[i]] = 1;
}
}
for (int i = 0; i < n; ++i)
{
if (record[arr[i]] % 2 == 0)
{
// Found first element which are appear odd times
cout << arr[i] << endl;
return;
}
}
cout << "No Result" << endl;
}
};
int main()
{
Searching *task = new Searching();
// Array of integer elements
int arr[] = {
2 , 3 , 2 , 4 , 6 , 2 , 6 , 7 , 3 , 4 , 5 , 3
};
// Get the length
int n = sizeof(arr) / sizeof(arr[0]);
// Find resultant element
// First element which occurs Even number times
// Here 4 occurring 2 times
task->firstEvenOccurs(arr, n);
return 0;
}
Output
4
// Include namespace system
using System;
using System.Collections.Generic;
// Csharp program for
// Find the first element that occurs an even number of times in an array
public class Searching
{
public void firstEvenOccurs(int[] arr, int n)
{
// This record is used to collect frequency of elements
Dictionary < int, int > record = new Dictionary < int, int > ();
for (int i = 0; i < n; ++i)
{
if (record.ContainsKey(arr[i]))
{
// Increase frequency
record[arr[i]] = record[arr[i]] + 1;
}
else
{
// Add new key value
record.Add(arr[i], 1);
}
}
for (int i = 0; i < n; ++i)
{
if (record[arr[i]] % 2 == 0)
{
// Found first element which are appear odd times
Console.WriteLine(arr[i]);
return;
}
}
Console.WriteLine("No Result");
}
public static void Main(String[] args)
{
Searching task = new Searching();
// Array of integer elements
int[] arr = {
2 , 3 , 2 , 4 , 6 , 2 , 6 , 7 , 3 , 4 , 5 , 3
};
// Get the length
int n = arr.Length;
// Find resultant element
// First element which occurs Even number times
// Here 4 occurring 2 times
task.firstEvenOccurs(arr, n);
}
}
Output
4
package main
import "fmt"
// Go program for
// Find the first element that occurs an even number of times in an array
type Searching struct {}
func getSearching() * Searching {
var me *Searching = &Searching {}
return me
}
func(this Searching) firstEvenOccurs(arr[] int, n int) {
// This record is used to collect frequency of elements
var record = make(map[int] int)
for i := 0 ; i < n ; i++ {
if _, found := record[arr[i]] ; found {
// Increase frequency
record[arr[i]] = record[arr[i]] + 1
} else {
// Add new key value
record[arr[i]] = 1
}
}
for i := 0 ; i < n ; i++ {
if record[arr[i]] % 2 == 0 {
// Found first element which are appear odd times
fmt.Println(arr[i])
return
}
}
fmt.Println("No Result")
}
func main() {
var task * Searching = getSearching()
// Array of integer elements
var arr = [] int {
2,
3,
2,
4,
6,
2,
6,
7,
3,
4,
5,
3,
}
// Get the length
var n int = len(arr)
// Find resultant element
// First element which occurs Even number times
// Here 4 occurring 2 times
task.firstEvenOccurs(arr, n)
}
Output
4
<?php
// Php program for
// Find the first element that occurs an even number of times in an array
class Searching
{
public function firstEvenOccurs($arr, $n)
{
// This record is used to collect frequency of elements
$record = array();
for ($i = 0; $i < $n; ++$i)
{
if (array_key_exists($arr[$i], $record))
{
// Increase frequency
$record[$arr[$i]] = $record[$arr[$i]] + 1;
}
else
{
// Add new key value
$record[$arr[$i]] = 1;
}
}
for ($i = 0; $i < $n; ++$i)
{
if ($record[$arr[$i]] % 2 == 0)
{
// Found first element which are appear odd times
echo($arr[$i]."\n");
return;
}
}
echo("No Result".
"\n");
}
}
function main()
{
$task = new Searching();
// Array of integer elements
$arr = array(2, 3, 2, 4, 6, 2, 6, 7, 3, 4, 5, 3);
// Get the length
$n = count($arr);
// Find resultant element
// First element which occurs Even number times
// Here 4 occurring 2 times
$task->firstEvenOccurs($arr, $n);
}
main();
Output
4
// Node JS program for
// Find the first element that occurs an even number of times in an array
class Searching
{
firstEvenOccurs(arr, n)
{
// This record is used to collect frequency of elements
var record = new Map();
for (var i = 0; i < n; ++i)
{
if (record.has(arr[i]))
{
// Increase frequency
record.set(arr[i], record.get(arr[i]) + 1);
}
else
{
// Add new key value
record.set(arr[i], 1);
}
}
for (var i = 0; i < n; ++i)
{
if (record.get(arr[i]) % 2 == 0)
{
// Found first element which are appear odd times
console.log(arr[i]);
return;
}
}
console.log("No Result");
}
}
function main()
{
var task = new Searching();
// Array of integer elements
var arr = [2, 3, 2, 4, 6, 2, 6, 7, 3, 4, 5, 3];
// Get the length
var n = arr.length;
// Find resultant element
// First element which occurs Even number times
// Here 4 occurring 2 times
task.firstEvenOccurs(arr, n);
}
main();
Output
4
# Python 3 program for
# Find the first element that occurs an even number of times in an array
class Searching :
def firstEvenOccurs(self, arr, n) :
# This record is used to collect frequency of elements
record = dict()
i = 0
while (i < n) :
if ((arr[i] in record.keys())) :
# Increase frequency
record[arr[i]] = record.get(arr[i]) + 1
else :
# Add new key value
record[arr[i]] = 1
i += 1
i = 0
while (i < n) :
if (record.get(arr[i]) % 2 == 0) :
# Found first element which are appear odd times
print(arr[i])
return
i += 1
print("No Result")
def main() :
task = Searching()
# Array of integer elements
arr = [2, 3, 2, 4, 6, 2, 6, 7, 3, 4, 5, 3]
# Get the length
n = len(arr)
# Find resultant element
# First element which occurs Even number times
# Here 4 occurring 2 times
task.firstEvenOccurs(arr, n)
if __name__ == "__main__": main()
Output
4
# Ruby program for
# Find the first element that occurs an even number of times in an array
class Searching
def firstEvenOccurs(arr, n)
# This record is used to collect frequency of elements
record = Hash.new()
i = 0
while (i < n)
if (record.key?(arr[i]))
# Increase frequency
record[arr[i]] = record[arr[i]] + 1
else
# Add new key value
record[arr[i]] = 1
end
i += 1
end
i = 0
while (i < n)
if (record[arr[i]] % 2 == 0)
# Found first element which are appear odd times
print(arr[i], "\n")
return
end
i += 1
end
print("No Result", "\n")
end
end
def main()
task = Searching.new()
# Array of integer elements
arr = [2, 3, 2, 4, 6, 2, 6, 7, 3, 4, 5, 3]
# Get the length
n = arr.length
# Find resultant element
# First element which occurs Even number times
# Here 4 occurring 2 times
task.firstEvenOccurs(arr, n)
end
main()
Output
4
import scala.collection.mutable._;
// Scala program for
// Find the first element that occurs an even number of times in an array
class Searching()
{
def firstEvenOccurs(arr: Array[Int], n: Int): Unit = {
// This record is used to collect frequency of elements
var record: HashMap[Int, Int] = new HashMap[Int, Int]();
var i: Int = 0;
while (i < n)
{
if (record.contains(arr(i)))
{
// Increase frequency
record.addOne(arr(i), record.get(arr(i)).get + 1);
}
else
{
// Add new key value
record.addOne(arr(i), 1);
}
i += 1;
}
i = 0;
while (i < n)
{
if (record.get(arr(i)).get % 2 == 0)
{
// Found first element which are appear odd times
println(arr(i));
return;
}
i += 1;
}
println("No Result");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Searching = new Searching();
// Array of integer elements
var arr: Array[Int] = Array(2, 3, 2, 4, 6, 2, 6, 7, 3, 4, 5, 3);
// Get the length
var n: Int = arr.length;
// Find resultant element
// First element which occurs Even number times
// Here 4 occurring 2 times
task.firstEvenOccurs(arr, n);
}
}
Output
4
import Foundation;
// Swift 4 program for
// Find the first element that occurs an even number of times in an array
class Searching
{
func firstEvenOccurs(_ arr: [Int], _ n: Int)
{
// This record is used to collect frequency of elements
var record = [Int : Int]();
var i: Int = 0;
while (i < n)
{
if (record.keys.contains(arr[i]))
{
// Increase frequency
record[arr[i]] = record[arr[i]]! + 1;
}
else
{
// Add new key value
record[arr[i]] = 1;
}
i += 1;
}
i = 0;
while (i < n)
{
if (record[arr[i]]! % 2 == 0)
{
// Found first element which are appear odd times
print(arr[i]);
return;
}
i += 1;
}
print("No Result");
}
}
func main()
{
let task: Searching = Searching();
// Array of integer elements
let arr: [Int] = [2, 3, 2, 4, 6, 2, 6, 7, 3, 4, 5, 3];
// Get the length
let n: Int = arr.count;
// Find resultant element
// First element which occurs Even number times
// Here 4 occurring 2 times
task.firstEvenOccurs(arr, n);
}
main();
Output
4
// Kotlin program for
// Find the first element that occurs an even number of times in an array
class Searching
{
fun firstEvenOccurs(arr: Array < Int > , n: Int): Unit
{
// This record is used to collect frequency of elements
var record = HashMap < Int, Int > ();
var i: Int = 0;
while (i < n)
{
if (record.containsKey(arr[i]))
{
// Increase frequency
record.put(arr[i], record.getValue(arr[i]) + 1);
}
else
{
// Add new key value
record.put(arr[i], 1);
}
i += 1;
}
i = 0;
while (i < n)
{
if (record.getValue(arr[i]) % 2 == 0)
{
// Found first element which are appear odd times
println(arr[i]);
return;
}
i += 1;
}
println("No Result");
}
}
fun main(args: Array < String > ): Unit
{
val task: Searching = Searching();
// Array of integer elements
val arr: Array < Int > = arrayOf(2, 3, 2, 4, 6, 2, 6, 7, 3, 4, 5, 3);
// Get the length
val n: Int = arr.count();
// Find resultant element
// First element which occurs Even number times
// Here 4 occurring 2 times
task.firstEvenOccurs(arr, n);
}
Output
4

In given image shows ruby implementation.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment