View Problem
From a List Produce a List of Duplicate Entries
Taking a list:
Write the code to produce a list of duplicates in the list:
Submit a new solution for clojure, cpp, csharp, erlang ...
["andrew", "bob", "chris", "bob"]
Write the code to produce a list of duplicates in the list:
["bob"]
cpp C++0x/C++11 (gcc >=4.6)
vector<string> lst = { "andrew", "bob", "chris", "bob" };
vector<string> lst_no_dups;
vector<string> tmp;
vector<string> dups;
sort(lst.begin(), lst.end());
unique_copy(lst.begin(), lst.end(), back_inserter(lst_no_dups));
set_difference(lst.begin(), lst.end(),
lst_no_dups.begin(), lst_no_dups.end(),
back_inserter(tmp));
unique_copy(tmp.begin(), tmp.end(), back_inserter(dups));
cout << dups << endl;
vector<string> lst_no_dups;
vector<string> tmp;
vector<string> dups;
sort(lst.begin(), lst.end());
unique_copy(lst.begin(), lst.end(), back_inserter(lst_no_dups));
set_difference(lst.begin(), lst.end(),
lst_no_dups.begin(), lst_no_dups.end(),
back_inserter(tmp));
unique_copy(tmp.begin(), tmp.end(), back_inserter(dups));
cout << dups << endl;
ocaml
let rem v lst =
let rec aux acc = function
| [] -> List.rev acc
| x::xs ->
if compare v x = 0
then aux acc xs
else aux (x::acc) xs
in
aux [] lst
(** in case of a match, returns a list with the duplicate(s) removed *)
let rec mem_rem v lst =
let rec aux acc = function
| [] -> None
| x::xs ->
if compare v x = 0
then Some(List.rev_append acc (rem v xs))
else aux (x::acc) xs
in
aux [] lst
let duplicates lst =
let rec aux acc = function
| [] -> List.rev acc
| x::xs ->
match mem_rem x xs with
| Some ret -> aux (x::acc) ret
| None -> aux acc xs
in
aux [] lst
let () =
let lst = ["andrew"; "bob"; "chris"; "bob"; "mike"; "peter"; "bob"] in
let dup = duplicates lst in
List.iter print_endline dup
let rec aux acc = function
| [] -> List.rev acc
| x::xs ->
if compare v x = 0
then aux acc xs
else aux (x::acc) xs
in
aux [] lst
(** in case of a match, returns a list with the duplicate(s) removed *)
let rec mem_rem v lst =
let rec aux acc = function
| [] -> None
| x::xs ->
if compare v x = 0
then Some(List.rev_append acc (rem v xs))
else aux (x::acc) xs
in
aux [] lst
let duplicates lst =
let rec aux acc = function
| [] -> List.rev acc
| x::xs ->
match mem_rem x xs with
| Some ret -> aux (x::acc) ret
| None -> aux acc xs
in
aux [] lst
let () =
let lst = ["andrew"; "bob"; "chris"; "bob"; "mike"; "peter"; "bob"] in
let dup = duplicates lst in
List.iter print_endline dup
ocaml
(* Using standard (functorized) sets *)
module SetTools(ASet: Set.S) =
struct
let find_duplicates l =
let rec aux l seen acc =
match l with
| [] -> acc
| h :: q ->
if ASet.mem h seen then
aux q seen (h :: acc)
else
aux q (ASet.add h seen) acc in
aux l (ASet.empty) []
end
module StringSet = Set.Make(String)
module StringSetTools = SetTools(StringSet)
StringSetTools.find_duplicates ["andrew"; "bob"; "chris"; "bob"];;
module SetTools(ASet: Set.S) =
struct
let find_duplicates l =
let rec aux l seen acc =
match l with
| [] -> acc
| h :: q ->
if ASet.mem h seen then
aux q seen (h :: acc)
else
aux q (ASet.add h seen) acc in
aux l (ASet.empty) []
end
module StringSet = Set.Make(String)
module StringSetTools = SetTools(StringSet)
StringSetTools.find_duplicates ["andrew"; "bob"; "chris"; "bob"];;
php
$arr = array('andrew', 'bob', 'chris', 'bob', 'chris', 'john', 'mary', 'lucy');
function match($a, $b)
{
// This is a separate function so you could include checking
// with loose comparisons or lowercase both strings, etc.
return $a === $b;
}
$results = array();
sort($arr);
for($i = 0; $i < count($arr) - 1; $i++)
{
if (match($arr[$i], $arr[$i+1]))
{
$results[] = $arr[$i];
$i++;
}
}
print_r($results);
function match($a, $b)
{
// This is a separate function so you could include checking
// with loose comparisons or lowercase both strings, etc.
return $a === $b;
}
$results = array();
sort($arr);
for($i = 0; $i < count($arr) - 1; $i++)
{
if (match($arr[$i], $arr[$i+1]))
{
$results[] = $arr[$i];
$i++;
}
}
print_r($results);
php
$arr = array('lucy', 'andrew', 'lucy', 'bob', 'chris', 'bob', 'chris', 'john', 'mary');
function match($a, $b)
{
// This is a separate function so you could include checking
// with loose comparisons or lowercase both strings, etc.
return $a === $b;
}
$results = array();
for($i = 0; $i < count($arr); $i++)
{
if (isset($results[$arr[$i]]))
{
$results[$arr[$i]]++;
}
else
{
$results[$arr[$i]] = 0;
}
}
$out = array();
foreach($results as $name => $count)
{
echo $name . ':' . $count . '<br />';
if ($count > 0)
{
$out[] = $name;
}
}
print_r($out);
function match($a, $b)
{
// This is a separate function so you could include checking
// with loose comparisons or lowercase both strings, etc.
return $a === $b;
}
$results = array();
for($i = 0; $i < count($arr); $i++)
{
if (isset($results[$arr[$i]]))
{
$results[$arr[$i]]++;
}
else
{
$results[$arr[$i]] = 0;
}
}
$out = array();
foreach($results as $name => $count)
{
echo $name . ':' . $count . '<br />';
if ($count > 0)
{
$out[] = $name;
}
}
print_r($out);
Submit a new solution for clojure, cpp, csharp, erlang ...




